Webpack 5不再对节点核心模块进行自动填充。 请问怎么修?
break CHANGE:默认情况下,webpack < 5用于为node.js核心模块包含polyfills。 现在情况已经不同了。验证你是否需要这个模块,并为它配置一个填充。
Webpack 5不再对节点核心模块进行自动填充。 请问怎么修?
break CHANGE:默认情况下,webpack < 5用于为node.js核心模块包含polyfills。 现在情况已经不同了。验证你是否需要这个模块,并为它配置一个填充。
当前回答
看起来你已经使用了默认情况下不包含在webpack构建中的path包。对我来说,像这样扩展webpack配置有帮助:
{
// rest of the webpack config
resolve: {
// ... rest of the resolve config
fallback: {
"path": require.resolve("path-browserify")
}
},
}
也可以通过npm install path-browserify——save-dev或yarn添加path-browserify——dev来安装path-browserify。
其他回答
当从webpack v4升级到v5时,我也得到了这些错误。 通过对webpack.config.js进行以下更改来解决
添加的决心。回退财产
删除节点属性
{
resolve: {
modules: [...],
fallback: {
"fs": false,
"tls": false,
"net": false,
"path": false,
"zlib": false,
"http": false,
"https": false,
"stream": false,
"crypto": false,
"crypto-browserify": require.resolve('crypto-browserify'), //if you want to use this module also don't forget npm i crypto-browserify
}
},
entry: [...],
output: {...},
module: {
rules: [...]
},
plugins: [...],
optimization: {
minimizer: [...],
},
// node: {
// fs: 'empty',
// net: 'empty',
// tls: 'empty'
// },
}
从v4升级到v5 => https://webpack.js.org/migrate/5/#clean-up-configuration
我在create-react-app中遇到了这个问题
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"web-vitals": "^2.1.2"
我通过将react-scripts更改为4.0.3版本来解决这个问题。
对我来说,我只是删除了一个未使用的导入,称为:
import res from "express/lib/response"
它把它修好了!
你需要React => v17 React脚本=> v5 webpack => v5
解决问题
1)安装
"fs": "^2.0.0", // npm i fs
"assert": "^2.0.0", // npm i assert
"https-browserify": "^1.0.0", // npm i https-browserify
"os": "^0.1.2", // npm i os
"os-browserify": "^0.3.0", // npm i os-browserify
"react-app-rewired": "^2.1.9", //npm i react-app-rewired
"stream-browserify": "^3.0.0", // stream-browserify
"stream-http": "^3.2.0", //stream-http
2)在根目录下创建config-override .js 图像
3)在config-override .js中添加配置
const webpack = require('webpack');
module.exports = function override(config, env) {
config.resolve.fallback = {
url: require.resolve('url'),
fs: require.resolve('fs'),
assert: require.resolve('assert'),
crypto: require.resolve('crypto-browserify'),
http: require.resolve('stream-http'),
https: require.resolve('https-browserify'),
os: require.resolve('os-browserify/browser'),
buffer: require.resolve('buffer'),
stream: require.resolve('stream-browserify'),
};
config.plugins.push(
new webpack.ProvidePlugin({
process: 'process/browser',
Buffer: ['buffer', 'Buffer'],
}),
);
return config;
}
3)更改packages.json
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-app-rewired eject"
}
问题解决了:)
你想用在
const nodeExternals = require('webpack-node-externals');
在webpack.config.js中添加
target: "node",
devtool: "source-map",
externals: [nodeExternals()],