问题
I get an error whilst running my node index.js here is my code
const botconfig = require("./botconfig.json");
const Discord = require("discord.js");
const bot = new Discord.Client({disableEveryone: true});
bot.on("ready", async () => {
console.log(`${bot.user.username} is online!`);
}};
bot.login(botconfig.token);
here is my error message
SyntaxError: missing ) after argument list
at new Script (vm.js:51:7)
at createScript (vm.js:138:10)
at Object.runInThisContext (vm.js:199:10)
at Module._compile (module.js:624:28)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
at startup (bootstrap_node.js:190:16)
hope you can help!
回答1:
If you see an error at new Script (vm.js:51:7), it means there's an error in a custom script that you passed to vm.js, the Node module that communicates with the V8 Virtual Machine.
new Script in vm.js is simply evaluating your code.
So you need to work out what the fault is in the code you passed to the V8 virtual machine. If you run the file directly (e.g. node some/path/some_file.js) you should get a pointer to the actual fault that looks like this:
YourPC:your-directory you$ node some/path/some_file.js
/some/system/path/your-directory./some/path/some_file.js:123
}};
^
SyntaxError: missing ) after argument list
at new Script (vm.js:51:7)
at createScript (vm.js:138:10)
at Object.runInThisContext (vm.js:199:10)
The part above the error message with the ^ caret shows you the faulty point in your own code.
In your case, it's pretty easy to spot: you have a }}; that should be a });.
If you have code that seems 100% fine but encounters this error, like @maevanapcontact's failing arrow functions, maybe you're using an old version of Node with an old version of V8 that didn't support that ECMAScript feature. Arrow functions didn't have complete support until Node version 6.
回答2:
I had the same error as you and I have fixed it by using function(){}; instead of () =>.
I don't really know why it doesn't work with arrow functions, but it did the job for me like that.
回答3:
I found it helpful to add a break-point in vm.js where the error is thrown. (click on the filename link in the stack trace). Reload the page and then inspect the local variables. The filename variable will give the the full name of the js file that caused the error.
Unfortunately this isn't enough to narrow down the exact line, so I ended up deleting parts of the file until it would compile. From there I was able to narrow what was causing the error. In my case, my IDE linter wasn't giving me any hints either.
回答4:
The following helped me.
Delete all node_modules
rm -rf node_modules/
and then install
npm install
来源:https://stackoverflow.com/questions/48875059/syntax-error-at-new-script-vm-js517-whilst-running-code-for-a-discord-bot-in