Error: Cannot find module './undefined' with discord-buttons, discord.js, TypeScrpit and webpack

Error: Cannot find module './undefined' with discord-buttons, discord.js, TypeScrpit and webpack

我为我的 Discord 机器人使用 TypeScript 和 Webpack。它过去编译没有问题,直到我尝试使用 discord-buttons.

我发现了一些关于 discord.js、TypesScript 和 Webpack 的问题和答案。但是他们中没有人谈论不和谐按钮。

我的 webpack 配置文件中的 module.exports 如下所示:

entry: {
    index: {
      import: "./src/index.ts",
      dependOn: ["discord", "buttons"]
    },
    discord: "discord.js",
    buttons: "discord-buttons"
},
resolve: {
    extensions: ['.tsx', '.ts', '.js'],
},
output: {
    filename: "[name].bundle.js",
    path: path.resolve(__dirname, "dist")
},
module: {
    rules: [{
      test: /\.tsx?$/,
      use: 'ts-loader',
      exclude: /node_modules/,
    }, ],
},
target: "node",
externals: [nodeExternals()],

我的 index.ts 文件对 discord-buttons 没有任何作用,但导入它并使用 disbut

import { Client, Message, MessageEmbed, TextChannel } from "discord.js"
import disbut from 'discord-buttons'

const token:string = `token`
const client: Client = new Client()
disbut(client)

client.login(token)

当我使用 Webpack 构建时(在开发模式下),它说没有错误,但是当我 运行 index.bundle.js

> webpack --mode development --config webpack.config.dev.js

asset index.bundle.js 75.4 KiB [emitted] (name: index)
asset buttons.bundle.js 6.75 KiB [compared for emit] (name: buttons)
asset discord.bundle.js 6.72 KiB [compared for emit] (name: discord)
runtime modules 5.42 KiB 16 modules
built modules 62.7 KiB [built]
  cacheable modules 62.6 KiB
    modules by path ./src/modules/*.ts 48.5 KiB
      ./src/modules/CommandManager.ts 19.8 KiB [built] [code generated]
      ./src/modules/OutputManager.ts 2.05 KiB [built] [code generated]
      ./src/modules/LogManager.ts 3.59 KiB [built] [code generated]
      ./src/modules/FirebaseManager.ts 17.9 KiB [built] [code generated]
      ./src/modules/SpikeChannels.ts 5.17 KiB [built] [code generated]
    ./src/index.ts 11.8 KiB [built] [code generated]
    ./spike-database-firebase-admin-ETC.json 2.26 KiB [built] [code generated]
  external "discord.js" 42 bytes [built] [code generated]
  external "discord-buttons" 42 bytes [built] [code generated]
  external "fastest-levenshtein" 42 bytes [built] [code generated]
  external "firebase-admin" 42 bytes [built] [code generated]
webpack 5.46.0 compiled successfully in 4242 ms

> spike-bot@1.0.0 bot
> node ./dist/index.bundle.js

node:internal/modules/cjs/loader:930
  throw err;
  ^

Error: Cannot find module './undefined'
Require stack:
- /home/eban/PATH_TO_PROJECT/dist/discord.bundle.js
- /home/eban/PATH_TO_PROJECT/dist/index.bundle.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:927:15)
    at Function.Module._load (node:internal/modules/cjs/loader:772:27)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at Object.__webpack_require__.f.require (/home/eban/PATH_TO_PROJECT/dist/discord.bundle.js:157:28)
    at /home/eban/PATH_TO_PROJECT/dist/discord.bundle.js:84:40
    at Array.reduce (<anonymous>)
    at Function.__webpack_require__.e (/home/eban/PATH_TO_PROJECT/dist/discord.bundle.js:83:67)
    at Array.map (<anonymous>)
    at Function.__webpack_require__.X (/home/eban/PATH_TO_PROJECT/dist/discord.bundle.js:121:22) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/PATH_TO_PROJECT/dist/discord.bundle.js',
    '/home/PATH_TO_PROJECT/dist/index.bundle.js'
  ]
}

问题似乎是您为外部模块添加了入口点,即您的 discordbuttons 入口点。入口点应该用于您自己的脚本,通常不是外部脚本。如果您不打算 运行 多个进程,那么您也只需要一个入口点。