嗨,伙计们,我是一个新手在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 }
图像包含输出
6条答案
按热度按时间x0fgdtte1#
我解决了这些错误,但我的应用程序没有呈现。如果你有兴趣清除这些错误,你可以直接粘贴代码到
your-project/node_modules/react-scripts/config/webpack.config.js
,但这些更改可能会在重建应用程序后被覆盖。在module中找到。exports对象解析并写入回退,在您的情况下是“crypto”:require.resolve("crypto-browserify")
.并安装依赖项
npm install crypto-browserify
。或者你也可以使用react-app-rewired添加回退,如Github https://github.com/facebook/create-react-app/issues/11756中所述安装react-app-rewired,在项目的根目录下创建
config-overrides.js
文件。在package.json中将脚本从
'start': 'react-scripts start'
更改为'start': 'react-app-rewired start'
。然后启动项目npm run start或yarn startnnvyjq4y2#
我是这样解决我的问题的:
fdbelqdn3#
要在reactjs中使用webpack 5中的polyfill,请执行以下步骤:
1.首先安装
npm install node-polyfill-webpack-plugin
模块。(参考链接:(第10页)1.然后转到webpack.config.js --〉节点模块-〉React脚本-〉配置-〉webpack.config.js
1.然后添加下面的代码:
huwehgph4#
这看起来像是许多软件包(包括web 3)的新问题,因为这些软件包在不为polyfils添加回退的情况下与Webpack v5不兼容。
此处注明的问题:https://github.com/facebook/create-react-app/issues/11756
我通过在webpack.config.js文件中添加回退来解决这个问题;
但在构建过程中出现编译错误:
这是通过添加到我的.env文件解决的;
希望这对你有帮助。
ggazkfy85#
由于我没有足够的声望来 * 编辑 * 或 * 评论 * 这个answer,我不得不创建一个新的答案,为我工作。基本上一个也需要安装
readline
。即:
ndasle7k6#
在我的情况下,一个未使用的组件导入导致了错误。简单地删除不需要的组件导入。
从“React”导入{ReactDOM}