gpt4 book ai didi

node.js - webpack 5 错误 "Can' t 解析 'uglify-js' , '@swc/core' , 'esbuild'

转载 作者:行者123 更新时间:2023-12-05 03:29:26 30 4
gpt4 key购买 nike

我在使用 webpack 5、express、github 操作时遇到了一些错误。我使用 github actions 因为想使用 github secrets。另外,我使用 webpack 是因为使用了代码中的 secret 。

无论如何,我在下面遇到了错误,

ERROR in ./node_modules/terser-webpack-plugin/dist/utils.js 479:6-26
Module not found: Error: Can't resolve 'uglify-js' in '/Users/luna/workspace/express_api/node_modules/terser-webpack-plugin/dist'
@ ./node_modules/terser-webpack-plugin/dist/index.js 27:4-22
@ ./webpack.config.js 2:21-53
@ ./app.js 13:15-45

ERROR in ./node_modules/terser-webpack-plugin/dist/utils.js 557:14-34
Module not found: Error: Can't resolve '@swc/core' in '/Users/luna/workspace/express_api/node_modules/terser-webpack-plugin/dist'
@ ./node_modules/terser-webpack-plugin/dist/index.js 27:4-22
@ ./webpack.config.js 2:21-53
@ ./app.js 13:15-45

ERROR in ./node_modules/terser-webpack-plugin/dist/utils.js 635:18-36
Module not found: Error: Can't resolve 'esbuild' in '/Users/luna/workspace/express_api/node_modules/terser-webpack-plugin/dist'
@ ./node_modules/terser-webpack-plugin/dist/index.js 27:4-22
@ ./webpack.config.js 2:21-53
@ ./app.js 13:15-45

此外,收到 15 条警告。如果你想要警告,请告诉我。

问题是什么?

下面是webpack.config.js

const webpack = require("webpack");

module.exports = {
target: "async-node",
mode: "production",
entry: "./app.js",
output: {
path: "/dist",
filename: "main.js",
},
plugins: [
new webpack.DefinePlugin({
"process.env.PA_URL": JSON.stringify(process.env.PA_URL),
"process.env.PA5_EMAIL": JSON.stringify(process.env.PA5_EMAIL),
"process.env.PA5_PASSWORD": JSON.stringify(process.env.PA5_PASSWORD),
}),
],
};

webpack版本不对?

最佳答案

你正在捆绑你的入口文件,还有你的 ./app.js 需要的任何 node_modules,这反过来又试图需要一堆第三方的东西,这是不可用的 - 因此缺少模块的错误。

通过扩展你的 webpack 配置来排除那些 node_modules:yarn add -D webpack-node-externals(参见:https://www.npmjs.com/package/webpack-node-externals)

const webpack = require("webpack");
const nodeExternals = require("webpack-node-externals");

module.exports = {
target: "async-node",
mode: "production",
entry: "./app.js",
externalsPresets: { node: true }, // <-- here
externals: [nodeExternals()], // <-- and here
...etc

这应该排除了它们,你应该可以开始了。

关于node.js - webpack 5 错误 "Can' t 解析 'uglify-js' , '@swc/core' , 'esbuild',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71013802/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com