SyntaxError: Unexpected token '??=' when hosting a Discord bot on heroku

SyntaxError: Unexpected token '??=' when hosting a Discord bot on heroku

我想在 Heroku 上托管一个我创建的(使用 Node.js)的 discord 机器人,但抛出该错误:

2021-11-05T00:00:10.334347+00:00 app[web.1]: > node .
2021-11-05T00:00:10.334348+00:00 app[web.1]: 
2021-11-05T00:00:10.393407+00:00 app[web.1]: /app/node_modules/discord.js/src/rest/APIRequest.js:33
2021-11-05T00:00:10.393408+00:00 app[web.1]:     agent ??= new https.Agent({ ...this.client.options.http.agent, keepAlive: true });
2021-11-05T00:00:10.393409+00:00 app[web.1]:           ^^^
2021-11-05T00:00:10.393409+00:00 app[web.1]: 
2021-11-05T00:00:10.393409+00:00 app[web.1]: SyntaxError: Unexpected token '??='
2021-11-05T00:00:10.393412+00:00 app[web.1]:     at wrapSafe (internal/modules/cjs/loader.js:1001:16)
2021-11-05T00:00:10.393413+00:00 app[web.1]:     at Module._compile (internal/modules/cjs/loader.js:1049:27)
2021-11-05T00:00:10.393413+00:00 app[web.1]:     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
2021-11-05T00:00:10.393413+00:00 app[web.1]:     at Module.load (internal/modules/cjs/loader.js:950:32)
2021-11-05T00:00:10.393414+00:00 app[web.1]:     at Function.Module._load (internal/modules/cjs/loader.js:790:12)
2021-11-05T00:00:10.393414+00:00 app[web.1]:     at Module.require (internal/modules/cjs/loader.js:974:19)
2021-11-05T00:00:10.393414+00:00 app[web.1]:     at require (internal/modules/cjs/helpers.js:93:18)
2021-11-05T00:00:10.393415+00:00 app[web.1]:     at Object.<anonymous> (/app/node_modules/discord.js/src/rest/RESTManager.js:4:20)
2021-11-05T00:00:10.393415+00:00 app[web.1]:     at Module._compile (internal/modules/cjs/loader.js:1085:14)
2021-11-05T00:00:10.393415+00:00 app[web.1]:     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
2021-11-05T00:00:10.400407+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-11-05T00:00:10.400581+00:00 app[web.1]: npm ERR! errno 1
2021-11-05T00:00:10.403958+00:00 app[web.1]: npm ERR! discord-bot@1.0.0 start: `node .`
2021-11-05T00:00:10.403998+00:00 app[web.1]: npm ERR! Exit status 1
2021-11-05T00:00:10.404053+00:00 app[web.1]: npm ERR! 
2021-11-05T00:00:10.404092+00:00 app[web.1]: npm ERR! Failed at the discord-bot@1.0.0 start script.
2021-11-05T00:00:10.404140+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-11-05T00:00:10.408367+00:00 app[web.1]: 
2021-11-05T00:00:10.408447+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-11-05T00:00:10.408486+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2021-11-05T00_00_10_404Z-debug.log
2021-11-05T00:00:10.592782+00:00 heroku[web.1]: Process exited with status 1
2021-11-05T00:00:10.917668+00:00 heroku[web.1]: State changed from starting to crashed

这是我的 package.json:

  "name": "discord-bot",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "scripts": {
    "start": "node .",
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "nodemon src/index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cli": "^1.0.1",
    "discord.js": "^13.3.1",
    "discord.js-commando": "^0.12.3"
  },
  "devDependencies": {
    "nodemon": "^2.0.14"
  }
} 

我也添加了一个 procfile : worker: node src/index.js

我有 Node.js 最新版本 (16.0)

我应该怎么做才能解决这个错误?

对我来说,你似乎没有传递令牌。

如果您从文件中获取令牌,请确保它被正确读取。

将节点更新到 >= v16.6.0。

https://github.com/discordjs/discord.js/issues/5119

我刚刚将 discord.js 更新到版本 12.5.3 并且成功了!我感谢所有的回答