Webpack 5不再对节点核心模块进行自动填充。 请问怎么修?

break CHANGE:默认情况下,webpack < 5用于为node.js核心模块包含polyfills。 现在情况已经不同了。验证你是否需要这个模块,并为它配置一个填充。


当前回答

npm install assert --save

npm install buffer --save

对于任何面临类似问题的人,只需安装缺少的模块。这些模块被报告为缺失,因为它们是node.js的一部分,但也可以通过npm单独使用。

其他回答

我尝试了这个https://stackoverflow.com/a/71803628/15658978解决方案,它解决了我的问题。

简单地运行以下2个命令

npm uninstall react-scripts
npm install react-scripts@4.0.3

我喜欢把事情简单化,在这里之前不需要运行npm run-script eject,所以我降级回react-scripts@4.0.3而不是5.0.0 到目前为止,Webpack仍然没有解决这个问题

可悲的是,因为这包括一堆退役的包裹。

使用node-polyfill-webpack-plugin重新添加对Node.js核心模块的支持:

安装包后,将以下内容添加到webpack.config.js中:

const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")

module.exports = {
    // Other rules...
    plugins: [
        new NodePolyfillPlugin()
    ]
}

我找到了一个适合我的解决方案:

卸载webpack 删除“package-lock”。json”文件 NPM安装webpack@4.44.2——force 包括在“套餐”中。Json”文件在你的“脚本”: "脚本":{ "start": "SET NODE_OPTIONS=——openssl-legacy-provider && react-scripts start", "SET NODE_OPTIONS=——openssl-legacy-provider && react-scripts build", “test”:“react-scripts test”, "eject": "react-scripts eject" }

对我来说,我只是删除了一个未使用的导入,称为:

import res from "express/lib/response"

它把它修好了!