javascriptnode.jsdiscorddiscord.js

node js discord bot is not works


    /home/discord/project/bloodmoon/node_modules/discord.js/src/client/Client.js:42
    } 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> (/home/discord/project/bloodmoon/node_modules/discord.js/src/index.js:8:11)

i'm interested in developing discord bot. so i tried it. first, it works. but, i moved the server and i setted it. and it not works! please give advise to me.

(please understand i can't write a English essay well. sorry)


Solution

  • If you check the discord.js source code, you can see that they try to silently ignore errors when their code requires the worker_threads module. They simply omit the usual (error) part after catch:

    try {
      // Test if worker threads module is present and used
      data = require('worker_threads').workerData || data;
    } catch {
      // Do nothing
    }
    

    However, this only works in newer versions of JavaScript; older ones will throw a SyntaxError. So, you must be using an older version of Node, because optional catch binding is only available in Node.js v10+.

    The following is an example in Node v8:

    // using Node v8
    try { error } catch { console.log('oops') }
    

    ⬇ result ⬇

    try { error } catch { console.log('oops') }
                        ^
    
    SyntaxError: Unexpected token {
    

    While it just works fine in Node v10+:

    // using Node v16
    try { error } catch { console.log('oops') }
    

    ⬇ result ⬇

    oops
    

    To solve this, you need to update your Node version to at least v12, as mentioned in the discord.js docs:

    v12 requires Node 12.x or higher, so make sure you're up-to-date. To check your Node version, use node -v in your terminal or command prompt, and if it's not high enough, update it! There are many resources online to help you with this step based on your host system.