【问题标题】:Syntax error at new Script (vm.js:51:7) whilst running code for a discord bot in javascript在 javascript 中运行不和谐机器人的代码时,新脚本 (vm.js:51:7) 出现语法错误
【发布时间】:2018-07-30 05:06:49
【问题描述】:

运行节点 index.js 时出现错误 这是我的代码

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);

这是我的错误信息

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)

希望你能帮忙!

【问题讨论】:

  • 你可能想把}};改成});
  • 现在我收到此错误错误:在 Function.Module._resolveFilename (module.js:555:15) at Function.Module._load (module.js :482:25) 在 Module.require (module.js:604:17) 在 require (internal/module.js:11:18) 在 Object. (C:\Users\manni\Desktop\DiscordBot\index .js:1:81) 在 Module._compile (module.js:660:30) 在 Object.Module._extensions..js (module.js:671:10) 在 Module.load (module.js:573:32) ) 在 tryModuleLoad (module.js:513:12) 在 Function.Module._load (module.js:505:3)
  • @RickyDom - 确保您的 botconfig.json 文件与您的 index.js 文件位于同一目录中

标签: javascript node.js discord


【解决方案1】:

我遇到了与您相同的错误,我已通过使用 function(){}; 而不是 () => 来修复它。 我真的不知道为什么它不适用于箭头功能,但它为我做了这样的工作。

【讨论】:

  • 您使用的是 Node 6 之前的 Node 版本吗?如果是这样,可能是因为Node didn't have complete arrow function support back then。如果不是,则可能是您没有发现的箭头函数中存在语法错误,您在更改函数类型时已修复此错误
  • 我使用了最新版本的 Node (9.7.1),并从文档中复制/粘贴了函数。我做了很多检查,但它仍然不能使用箭头函数
【解决方案2】:

如果您看到错误 at new Script (vm.js:51:7),则表示您传递给 vm.js 的自定义脚本(Node module that communicates with the V8 Virtual Machine)中存在错误。

vm.js 中的new Script 只是在评估您的代码。

因此,您需要找出您传递给 V8 虚拟机的代码的问题所在。如果您直接运行该文件(例如node some/path/some_file.js),您应该得到指向实际故障的指针,如下所示

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)

带有^ 插入符号的错误消息上方的部分显示了您自己代码中的错误点。

在你的情况下,很容易发现:你有一个}};,它应该是一个});

如果您的代码看起来 100% 正常但遇到此错误,例如 @maevanapcontact 的箭头函数失败,则可能您使用的是旧版本的 Node 和不支持该 ECMAScript 功能的旧版本 V8。 Arrow functions didn't have complete support until Node version 6.

【讨论】:

    【解决方案3】:

    我发现在引发错误的 vm.js 中添加断点很有帮助。 (单击堆栈跟踪中的文件名链接)。 重新加载页面,然后检查局部变量。 filename 变量会给出导致错误的 js 文件的全名。

    不幸的是,这不足以缩小确切的范围,所以我最终删除了部分文件,直到它编译为止。从那里我能够缩小导致错误的范围。就我而言,我的 IDE linter 也没有给我任何提示。

    【讨论】:

      【解决方案4】:

      以下对我有帮助。

      删除所有节点模块

      rm -rf node_modules/
      

      然后安装

      npm install
      

      【讨论】:

        猜你喜欢
        • 2018-04-07
        • 2020-12-03
        • 1970-01-01
        • 2020-08-29
        • 2018-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-20
        相关资源
        最近更新 更多