【问题标题】:discord.js typescript error - missing index.ts when executing 'ts-node index.ts'discord.js 打字稿错误 - 执行“ts-node index.ts”时缺少 index.ts
【发布时间】:2022-01-30 06:45:52
【问题描述】:

我正在使用 discord.js 和 typescript 创建一个不和谐的机器人,但是当我尝试打开机器人时,使用“ts-node index.ts”这是给我的错误:

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 操作系统的树莓派来托管这个机器人

【问题讨论】:

  • 请将您的代码和错误信息以文本形式发布。
  • 你的文件结构是什么样的?您是否在保存index.ts 文件的同一文件夹中运行ts-node index.ts
  • 在更新我的代码中的一些错误后,我忘记重新输入正确的目录。谢谢!但是有一个新错误,我发现它更令人困惑。更新了错误。

标签: javascript typescript discord.js


【解决方案1】:

您可能没有使用正确的节点版本。尝试升级它。较新的 discordjs 需要 atleasr v16.6。 看看这个帖子error when requiring discordjs - cannot find module 'node:events'。 我在安装 node16 时遇到问题的 32 位机器上遇到了同样的问题,所以我使用了 older discordjs 版本。

【讨论】:

  • 我正在使用运行 ubuntu aarch64 的树莓派。最好使用 discord.js 还是 discord.js.old? discord.js.old 是否需要不同的 js 语法,或者我可以保留我已经编写的代码吗?
  • 当然你可以只升级nodejs版本。如果你在 UBUNTU,应该是这个命令sudo npm cache clean -fsudo npm install -g n sudo n stable
【解决方案2】:

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

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

对我有用的解决方案是安装“nvm”并从那里安装 v16(文档中的解决方案 2)。

【讨论】:

    猜你喜欢
    • 2019-01-05
    • 2017-12-10
    • 2018-07-11
    • 2020-06-30
    • 2017-10-05
    • 2021-01-01
    • 1970-01-01
    • 2021-07-18
    • 2021-04-21
    相关资源
    最近更新 更多