【问题标题】:Internal error when trying to require discord.js尝试要求 discord.js 时出现内部错误
【发布时间】:2020-09-04 15:58:59
【问题描述】:

我正在尝试遵循 discord.js 官方网站给出的教程,但是当我尝试在我的代码中要求 discord.js 时,当我运行 node index.js 时会出现以下错误:

    /Users/[MY NAME]/Desktop/JimBot/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> (/Users/[MY NAME]/Desktop/JimBot/node_modules/discord.js/src/index.js:8:11)

我已按照文档的说明将 Node 更新到 12.2.0。我的 package.json 看起来像这样:

{
  "name": "jimbot",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node server.js"
  },
  "engines": {
    "node": "12.0.0"
  },
  "license": "ISC",
  "dependencies": {
    "discord.js": "^12.2.0",
    "eslint": "^7.0.0"
  }
}

感谢大家的帮助。

【问题讨论】:

  • 你能展示你的代码吗?
  • @Pepe_W​​orm 是:const Discord = require('discord.js'); const client = new Discord.Client(); client.once('ready', () =&gt; { console.log('Ready!'); }); client.login([token]); 我写的 [token] 是我的实际令牌。
  • 这是你现在的所有代码还是还有更多?另外,您是否保存了当前代码?
  • 就是这样。我正在逐字阅读教程。我让它在 heroku 上工作,但它不能在我的 (mac) 笔记本电脑上工作。

标签: discord.js


【解决方案1】:

我已经尝试解决这个问题 3 天了。我终于在这里找到了解决方案:https://stackoverflow.com/a/59796074/9725459

基本上在 package.json 文件中添加(或更新)您的引擎,如下所示:

"engines": {
    "node": "12.14.1",
    "npm": "6.13.4"
},

【讨论】:

    【解决方案2】:

    试试

    const Discord = require('discord.js'); 
    const client = new Discord.Client(); 
    
    client.on('ready', () => {
        console.log('Ready!');
    });
    
    client.login([token]);
    

    【讨论】:

    • 错误仍然存​​在。
    • @chokkore 你保存了吗?
    • 是的,我确实保存了它。我可能会为此制作一个虚拟机,以免其他任何干扰。
    猜你喜欢
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 2014-01-12
    • 2021-10-12
    • 2020-12-07
    相关资源
    最近更新 更多