ts节点找不到npm模块discord-api类型



当尝试使用命令ts-node deploy-commands.ts运行discord.js文件deploy-commands.ts时,ts节点会产生以下错误:

Error: Cannot find module 'discord.js/node_modules/discord-api-types'
Require stack:
- /Users/Cameron/discord.js/discordjs-guides/deploy-commands.ts
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933: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 (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/Cameron/discord.js/discordjs-guides/deploy-commands.ts:4:1)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Module.m._compile (/usr/local/lib/node_modules/ts-node/src/index.ts:1455:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Object.require.extensions.<computed> [as .ts] (/usr/local/lib/node_modules/ts-node/src/index.ts:1458:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/Cameron/discord.js/discordjs-guides/deploy-commands.ts' ]
}

尽管discord-api-types同时出现在node-modules文件夹和package.json中。在运行npm install discord-api-types和npm后,错误始终会重现,并且重新编码的discord-api类型是最新的。

Package.json依赖项:

"dependencies": {
"@discordjs/builders": "^0.12.0",
"@discordjs/rest": "^0.3.0",
"discord-api-types": "^0.29.0",
"discord.js": "^13.6.0",
"dotenv": "^16.0.0",
"nodemon": "^2.0.15",
"ts-node": "^10.7.0"
}

deploy-commands.ts:

import { SlashCommandBuilder } from "@discordjs/builders";
import { REST } from "@discordjs/rest";
import { version } from "discord.js";
import { Routes } from "discord.js/node_modules/discord-api-types";
const { token, guildID, clientID } = require('./process.json')
const commands = [
new SlashCommandBuilder().setName('ping').setDescription('Replies with pong.'),
new SlashCommandBuilder().setName('server').setDescription('Replies with server info.'),
new SlashCommandBuilder().setName('user').setDescription('Replies with user info.'),
]
.map(commands => JSON)
const rest = new REST({version: '9'}).setToken(token)
rest.put(Routes.applicationCommand(clientID, guildID), {body: { commands }})
.then(() => {
console.log('Successfully registered application commands')
})
.catch(console.error)

我认为您需要更改以下代码行:

import { Routes } from "discord.js/node_modules/discord-api-types";

因此使用其版本:

const rest = new REST({version: '9'}).setToken(token)

因此,您需要定义您使用的版本,尝试将导入更改为

import { Routes } from "discord-api-types/v9";

当我尝试测试斜杠命令文件时,你也可以这样做:

import { Routes } from "discord.js/node_modules/discord-api-types/v9";

我没有使用.ts,所以可能不起作用,但试试

最新更新