ts-node 找不到 npm 模块 discord-api-types

ts-node cannot find npm module discord-api-types

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

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 同时出现在节点模块文件夹和 package.json 中。在 运行ning npm install discord-api-types 和 npm resoding discord-api-types 是最新的之后,错误一直被重现。

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"
  }

部署-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,所以可能行不通,但试一试