vue项目webpack < 5 used to include polyfills for node.js core modules by default. This is no longer the case. Verify if you need this module and configure a polyfill for it

问题复现

启动vue@2.6版本的项目时突然报错,查看webpack文档。原来webpack 5 不再自动 polyfill Node.js 的核心模块,这意味着如果你在浏览器或类似的环境中运行的代码中使用它们,你必须从 NPM 中安装兼容的模块,并自己包含它们。文档地址

image-20240531173830928

解决方案

vue.config.js配置文件中如下配置。如果你想包含某个polyfill,则手动下载对应的包。否则设置为false。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
module.exports = defineConfig({
transpileDependencies: true, devServer: {
proxy: {
"/v1": {
target: ""
}
}
}, configureWebpack: {
resolve: {
fallback: {
'assert': false,
'process': false,
'url': false,
'path-browserify': false,
'util': false,
"fs": false,
"module": false,
"v8": false,
"path": require.resolve("path-browserify") // npm install path-browserify
}
}
}
})

或者下载插件node-polyfill-webpack-plugin,该插件Polyfill Webpack5 中的 Node.js 核心模块。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")

module.exports = defineConfig({
transpileDependencies: true, devServer: {
proxy: {
"/v1": {
target: ""
}
}
}, configureWebpack: {
resolve: {
fallback: {
"module": false,
"v8": false,
}
},
plugins: [
new NodePolyfillPlugin()
]
}
})
  • Copyrights © 2021-2024 Colourful

请我喝杯咖啡吧~

支付宝
微信