不和谐.js打字稿错误 - 执行'ts-node index.ts'时缺少 index.ts



我正在用discord.js和typescript创建一个discordbot,但当我试图使用'ts-node index.ts'打开bot时,我得到的错误是:

Error: Cannot find module 'node:events'
Require stack:
- /home/ether0p12348/RainCrown/node_modules/discord.js/src/client/BaseClient.js
- /home/ether0p12348/RainCrown/node_modules/discord.js/src/index.js
- /home/ether0p12348/RainCrown/index.ts
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/usr/local/lib/node_modules/ts-node/node_modules/@cspotcode/source-map-support/source-map-support.js:679:30)
at Function.Module._load (internal/modules/cjs/loader.js:667:27)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/home/ether0p12348/RainCrown/node_modules/discord.js/src/client/BaseClient.js:3:22)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/home/ether0p12348/RainCrown/node_modules/discord.js/src/client/BaseClient.js',
'/home/ether0p12348/RainCrown/node_modules/discord.js/src/index.js',
'/home/ether0p12348/RainCrown/index.ts'
]
}

这是我在机器人的根目录中的index.ts中的内容

const Discord = require('discord.js')
import DiscordJS, {Intents} from 'discord.js'
import dotenv from 'dotenv'
dotenv.config()
const client = new DiscordJS.Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
]
})
client.on('ready', () => {
console.log('RainCrown is ready!')
})
client.on('messageCreate', (message) => {
if (message.content === 'ping') {
message.reply({
content: 'pong',
})
}
})
client.login(process.env.TOKEN)

我所拥有的一切";。env";是我的代币(已编辑(

我的'ls'输出:index.ts node_modules package.json package-lock.json tsconfig.json

如果有人能指导我采取什么措施来解决这个错误,我将不胜感激。

ps:我正在使用ubuntu操作系统的树莓pi来托管这个机器人

您可能没有使用正确的节点版本。尝试升级。更新的discordjs需要atleasr v16.6。当需要discordjs时,请查看此帖子错误-找不到模块';node:events';。我在32位机器上遇到了同样的问题,在安装node16时遇到了问题,所以我使用了旧的不协调js版本。

正如Borecjeborec1所说,我只需要将node.js更新到16.6。这是帮助我实现这一目标的文件:

https://computingforgeeks.com/how-to-install-node-js-on-ubuntu-debian/

对我有效的解决方案是安装"nvm"并从那里安装v16(文档中的解决方案2(。

最新更新