我试图在Webpack 5中填充模块,但不工作(Reactjs)

ryhaxcpt  于 2022-11-13  发布在  Webpack
关注(0)|答案(6)|浏览(183)

嗨,伙计们,我是一个新手在React,当我开始我的项目,我得到了Wepback的V5错误信息

解决更新:https://github.com/facebook/create-react-app/issues/11756#issuecomment-1001162736

"我用的是什么"

Os: Win11
Node : v16
React:v17
React-script : v5
Webpack:v5

此错误消息包含

Crypto
Http
Https
Stream

错误输出

编译时出现问题:X

ERROR in ./node_modules/eth-lib/lib/bytes.js 9:193-227

Module not found: Error: Can't resolve 'crypto' in 'C:\Users\PC\Desktop\Portfolio\portfolio_app\node_modules\eth-lib\lib'

BREAKING CHANGE: 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.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }'
    - install 'crypto-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "crypto": false }

ERROR in ./node_modules/web3-eth-accounts/lib/index.js 31:74-91

Module not found: Error: Can't resolve 'crypto' in 'C:\Users\PC\Desktop\Portfolio\portfolio_app\node_modules\web3-eth-accounts\lib'

BREAKING CHANGE: 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.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }'
    - install 'crypto-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "crypto": false }

ERROR in ./node_modules/web3-eth-accounts/node_modules/eth-lib/lib/bytes.js 7:193-227

Module not found: Error: Can't resolve 'crypto' in 'C:\Users\PC\Desktop\Portfolio\portfolio_app\node_modules\web3-eth-accounts\node_modules\eth-lib\lib'

BREAKING CHANGE: 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.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }'
    - install 'crypto-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "crypto": false }

图像包含输出

Webpack5 Error Message

x0fgdtte

x0fgdtte1#

我解决了这些错误,但我的应用程序没有呈现。如果你有兴趣清除这些错误,你可以直接粘贴代码到your-project/node_modules/react-scripts/config/webpack.config.js,但这些更改可能会在重建应用程序后被覆盖。在module中找到。exports对象解析并写入回退,在您的情况下是“crypto”:require.resolve("crypto-browserify") .
并安装依赖项npm install crypto-browserify

resolve: {
//   fallback: {
//     "fs": false,
//     "tls": false,
//     "net": false,
//     "http": require.resolve("stream-http"),
//     "https": false,
//     "zlib": require.resolve("browserify-zlib") ,
//     "path": require.resolve("path-browserify"),
//     "stream": require.resolve("stream-browserify"),
//     "util": require.resolve("util/"),
       "crypto": require.resolve("crypto-browserify")
}

或者你也可以使用react-app-rewired添加回退,如Github https://github.com/facebook/create-react-app/issues/11756中所述安装react-app-rewired,在项目的根目录下创建config-overrides.js文件。

module.exports = function override (config, env) {
    console.log('override')
    let loaders = config.resolve
    loaders.fallback = {
        "fs": false,
        "tls": false,
        "net": false,
        "http": require.resolve("stream-http"),
        "https": false,
        "zlib": require.resolve("browserify-zlib") ,
        "path": require.resolve("path-browserify"),
        "stream": require.resolve("stream-browserify"),
        "util": require.resolve("util/"),
        "crypto": require.resolve("crypto-browserify")
    }
    
    return config
}

在package.json中将脚本从'start': 'react-scripts start'更改为'start': 'react-app-rewired start'。然后启动项目npm run start或yarn start

nnvyjq4y

nnvyjq4y2#

我是这样解决我的问题的:

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

fdbelqdn3#

要在reactjs中使用webpack 5中的polyfill,请执行以下步骤:
1.首先安装npm install node-polyfill-webpack-plugin模块。(参考链接:(第10页)
1.然后转到webpack.config.js --〉节点模块-〉React脚本-〉配置-〉webpack.config.js
1.然后添加下面的代码:

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

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

huwehgph

huwehgph4#

这看起来像是许多软件包(包括web 3)的新问题,因为这些软件包在不为polyfils添加回退的情况下与Webpack v5不兼容。
此处注明的问题:https://github.com/facebook/create-react-app/issues/11756
我通过在webpack.config.js文件中添加回退来解决这个问题;

module.exports = {
    resolve: {
        fallback: {
            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'),
            stream: require.resolve('stream-browserify'),
        },
    },
};

但在构建过程中出现编译错误:

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory" error

这是通过添加到我的.env文件解决的;

GENERATE_SOURCEMAP=false

希望这对你有帮助。

ggazkfy8

ggazkfy85#

由于我没有足够的声望来 * 编辑 * 或 * 评论 * 这个answer,我不得不创建一个新的答案,为我工作。基本上一个也需要安装readline
即:

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

ndasle7k6#

在我的情况下,一个未使用的组件导入导致了错误。简单地删除不需要的组件导入。
从“React”导入{ReactDOM}

相关问题