1

Issues:

I recently moved my code from a Windows machine to a VPS, everything seemed to be working on my PC, but when I moved to the VPS, I got the following error

/root/node_modules/discord.js/src/client/Client.js:41
    } catch {
            ^

SyntaxError: Unexpected token {
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/root/node_modules/discord.js/src/index.js:8:11)

Environment Details:

I am using Cloud Computing on Ubuntu 18.04 64x

I am using the command handler found here: https://anidiots.guide/first-bot/a-basic-command-handler

Any and all help will be greatly appreciated

rexfordkelly
  • 1,623
  • 10
  • 15
roh
  • 43
  • 6
  • 1
    [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Andreas Jul 09 '20 at 16:22
  • 2
    OP: You need to update Node to version 12 or above. – Lewis Jul 13 '20 at 02:24
  • 1
    @Christian technically, to at least 10. That's where optional catch binding is a thing. However, v12 of discord.js indeed requried node 12 or newer. – tipakA Jul 13 '20 at 06:16
  • @delta-v you may want to provide the environment details of each server, specifically the node versions in use in both environments, at the time of your initial post. – rexfordkelly Jul 13 '20 at 16:06

1 Answers1

2

Seeing as this question is currently locked, and not accepting new answers, I thought I would address a few observations on the incompatibilities between node versions, all due credit goes to the commenters: @tipakA and @Christian.


There are incompatibilities between versions of Node, it may simply be that you need to update your VPS's version of Node to a later version, one where the catch blocks are optional, or more precisely to one which satifies your dependancies, in light of the comment by @tipakA

v12 of discord.js indeed requried node 12 or newer - @tipakA

In addition to simply upgrading to a compatible version of Node, I would also suggest, you "preserve" the node version as a dependency in your package.json file, like:

"engines" : { 
     "node" : ">=0.12" 
} 

Ensure you, signal that your project will not work at all in lower versions of Node, with this flag

"engineStrict" : true

Now when you deploy and npm install, you will be warned of any discrepancies in Node versions.

Unfortunately it appears that support for "engines" is in flux between npm versions, for example they are deprecated, as of npm 3, but supported in npm v 5x..., so you may want to 'patch/polly-fill' the feature, as outlined in this article, all credit goes to 'Adam Bisek' the author of that article.

Also, this answer on StackOverflow, provides additional approaches to the Node version as dependency issue.


Alternatively, without having more insight into the stack, or the environment, I have to go with what looks obvious to me.

From the fact that this is throwing a SyntaxError I would look at where the SyntaxError is. To me the signature of your try catch looks off.

JavaScript has the following signature for a Try/Catch block:

try {
  try_statements
}
[catch (exception_var_1 if condition_1) { // non-standard
  catch_statements_1
}]
...
[catch (exception_var_2) {
  catch_statements_2
}]
[finally {
  finally_statements
}]

So you would need to have a try catch as such, like this

try {

  // attempt to perform something here...

} catch ( e ) {
 
  // do something with "e" or handle the error some how.

}

you can read up on this more here.

rexfordkelly
  • 1,623
  • 10
  • 15
  • 1
    This answer is correct for versions of node < 10.0.0. On 10.0.0 and newer, catch binding is [optional](https://node.green/#ES2019-misc-optional-catch-binding) – tipakA Jul 13 '20 at 06:14
  • @tipakA, thanks for zeroing in on the heart of the matter, I've updated my answer to incorporate your observations. – rexfordkelly Jul 13 '20 at 15:50