react项目 Can‘t resolve ‘stream‘ in ‘.../node_modules/cipher-base‘ 问题解决

react项目安装Ipfs-api模块以后报如下错误。

Module not found: Error: Can't resolve 'stream' in '/Users/domino/VscodeWeb3Projects/imooc-on-blockchain/node_modules/cipher-base'
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: { "stream": require.resolve("stream-browserify") }'
        - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "stream": false }
WARNING in ./node_modules/mutationobserver-shim/dist/mutationobserver.min.js
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map from '/User/domino/VscodeWeb3Projects/imooc-on-blockchain/node_modules/mutationobserver-shim/dist/MutationObserver.js' file: Error: ENOENT: no such file or directory, open '/Users/domino/VscodeWeb3Projects/imooc-on-blockchain/node_modules/mutationobserver-shim/dist/MutationObserver.js'

ERROR in ./node_modules/ip/lib/ip.js 5:9-22
Module not found: Error: Can't resolve 'os' in '/Users/domino/VscodeWeb3Projects/imooc-on-blockchain/node_modules/ip/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: { "os": require.resolve("os-browserify/browser") }'
        - install 'os-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "os": false }


原因:webpack <5 的版本曾经默认引入polyfills作为Nodejs核心模块,后来取消了。

解决方法:在nodejs.com查找 react-scripts的5.0以下的最新版本,将react-scripts的版本进行替换

#卸载react-scripts
% npm uninstall react-scripts

# 安装4.x react-scripts
% npm install react-scripts@4.0.3 --save

# 重新运行发现还需要安装 webpack 4.44.2版本的依赖
% npm run start

> imooc-on-blockchain@0.1.0 start
> react-app-rewired start


There might be a problem with the project dependency tree.
It is likely not a bug in Create React App, but something you need to fix locally.

The react-scripts package provided by Create React App requires a dependency:

  "webpack": "4.44.2"

Don't try to install it manually: your package manager does it automatically.


#  装一下
% npm install webpack@4.44.2

# 再启动发现出现了hash计算问题,在启动脚本上需要加一个配置
% npm run start             

> imooc-on-blockchain@0.1.0 start
> react-app-rewired start

ℹ 「wds」: Project is running at http://192.168.3.6/
ℹ 「wds」: webpack output is served from 
ℹ 「wds」: Content not from webpack is served from /Users/domino/VscodeWeb3Projects/imooc-on-blockchain/public
ℹ 「wds」: 404s will fallback to /
Starting the development server...

/Users/domino/VscodeWeb3Projects/imooc-on-blockchain/node_modules/react-scripts/scripts/start.js:19
  throw err;
  ^

Error: error:0308010C:digital envelope routines::unsupported
    at new Hash (node:internal/crypto/hash:71:19)
    at Object.createHash (node:crypto:133:10)
    at module.exports (/Users/domino/VscodeWeb3Projects/imooc-on-blockchain/node_modules/webpack/lib/util/createHash.js:135:53)
    at NormalModule._initBuildHash (/Users/domino/VscodeWeb3Projects/imooc-on-blockchain/node_modules/webpack/lib/NormalModule.js:417:16)
    at /Users/domino/VscodeWeb3Projects/imooc-on-blockchain/node_modules/webpack/lib/NormalModule.js:452:10
    at /Users/domino/VscodeWeb3Projects/imooc-on-blockchain/node_modules/webpack/lib/NormalModule.js:323:13
    at /Users/domino/VscodeWeb3Projects/imooc-on-blockchain/node_modules/loader-runner/lib/LoaderRunner.js:367:11
    at /Users/domino/VscodeWeb3Projects/imooc-on-blockchain/node_modules/loader-runner/lib/LoaderRunner.js:233:18
    at context.callback (/Users/domino/VscodeWeb3Projects/imooc-on-blockchain/node_modules/loader-runner/lib/LoaderRunner.js:111:13)
    at /Users/domino/VscodeWeb3Projects/imooc-on-blockchain/node_modules/babel-loader/lib/index.js:59:103 {
  opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],
  library: 'digital envelope routines',
  reason: 'unsupported',
  code: 'ERR_OSSL_EVP_UNSUPPORTED'
}

Node.js v18.8.0

启动脚本前增加 export NODE_OPTIONS=--openssl-legacy-provider

"scripts": {
    "start": "export NODE_OPTIONS=--openssl-legacy-provider && react-app-rewired start",

然后再启动就正常了,问题解决

参考文档

  • https://stackoverflow.com/questions/70591567/module-not-found-error-cant-resolve-fs-in-react

  • https://github.com/facebook/create-react-app/issues/11756

<w> [ReactRefreshPlugin] Hot Module Replacement (HMR) is not enabled! React Refresh requires HMR to function properly. asset main.js 2.28 KiB [emitted] (name: main) ./src/main.ts 39 bytes [built] [code generated] [1 error] ERROR in ./src/main.ts Module build failed (from ./node_modules/ts-loader/index.js): Error: ts-loader was supplied with an unexpected loader option: plugins Please take a look at the options you are supplying; the following are valid options: silent / logLevel / logInfoToStdOut / instance / compiler / context / configFile / transpileOnly / ignoreDiagnostics / errorFormatter / colors / compilerOptions / appendTsSuffixTo / appendTsxSuffixTo / onlyCompileBundledFiles / happyPackMode / getCustomTransformers / reportFiles / experimentalWatchApi / allowTsInNodeModules / experimentalFileCaching / projectReferences / resolveModuleName / resolveTypeReferenceDirective / useCaseSensitiveFileNames at validateLoaderOptions (/home/flexiv/workspace/Framework4.x/TrajEditor/TrajEditorDemo/node_modules/ts-loader/dist/index.js:166:19) at getLoaderOptions (/home/flexiv/workspace/Framework4.x/TrajEditor/TrajEditorDemo/node_modules/ts-loader/dist/index.js:123:5) at Object.loader (/home/flexiv/workspace/Framework4.x/TrajEditor/TrajEditorDemo/node_modules/ts-loader/dist/index.js:15:21) webpack 5.101.3 compiled with 1 error in 731 ms asset app.js 1.18 MiB [emitted] (name: main) 1 related asset asset index.html 232 bytes [compared for emit] runtime modules 1.28 KiB 2 modules modules by path ./node_modules/core-js-pure/ 26.8 KiB 56 modules modules by path ./node_modules/@pmmmwh/react-refresh-webpack-plugin/ 45.9 KiB 18 modules modules by path ./node_modules/react-dom/ 947 KiB 4 modules modules by path ./node_modules/html-entities/dist/commonjs/*.js 34.2 KiB 4 modules modules by path ./node_modules/react-refresh/ 12.3 KiB ./node_modules/react-refresh/runtime.js 218 bytes [built] [code generated] ./node_modules/react-refresh/cjs/react-refresh-runtime.development.js 12.1 KiB [built] [code generated] modules by path ./node_modules/react/ 44.2 KiB ./node_modules/react/index.js 186 bytes [built] [code generated] ./node_modules/react/cjs/react.development.js 44 KiB [built] [code generated] modules by path ./node_modules/scheduler/ 12 KiB ./node_modules/scheduler/index.js 194 bytes [built] [code generated] ./node_modules/scheduler/cjs/scheduler.development.js 11.9 KiB [built] [code generated] + 4 modules ERROR in ./node_modules/@pmmmwh/react-refresh-webpack-plugin/sockets/WDSSocket.js 7:36-93 Module not found: Error: Can&#39;t resolve &#39;webpack-dev-server/client/clients/SockJSClient&#39; in &#39;/home/flexiv/workspace/Framework4.x/TrajEditor/TrajEditorDemo/node_modules/@pmmmwh/react-refresh-webpack-plugin/sockets&#39; @ ./node_modules/@pmmmwh/react-refresh-webpack-plugin/client/ErrorOverlayEntry.js 79:4-28 85:8-32 ERROR in ./node_modules/@pmmmwh/react-refresh-webpack-plugin/sockets/WDSSocket.js 8:39-99 Module not found: Error: Can&#39;t resolve &#39;webpack-dev-server/client/clients/WebSocketClient&#39; in &#39;/home/flexiv/workspace/Framework4.x/TrajEditor/TrajEditorDemo/node_modules/@pmmmwh/react-refresh-webpack-plugin/sockets&#39; @ ./node_modules/@pmmmwh/react-refresh-webpack-plugin/client/ErrorOverlayEntry.js 79:4-28 85:8-32 ERROR in ./node_modules/@pmmmwh/react-refresh-webpack-plugin/sockets/WDSSocket.js 9:21-64 Module not found: Error: Can&#39;t resolve &#39;webpack-dev-server/client/socket&#39; in &#39;/home/flexiv/workspace/Framework4.x/TrajEditor/TrajEditorDemo/node_modules/@pmmmwh/react-refresh-webpack-plugin/sockets&#39; @ ./node_modules/@pmmmwh/react-refresh-webpack-plugin/client/ErrorOverlayEntry.js 79:4-28 85:8-32 3 errors have detailed information that is not shown. Use &#39;stats.errorDetails: true&#39; resp. &#39;--stats-error-details&#39; to show it. webpack 5.101.3 compiled with 3 errors in 976 ms
09-12
### 解决 HMR 未启用问题 要确保 HMR(Hot Module Replacement)启用,需要在 Webpack 配置中正确设置。在 `webpack.config.js` 或 `webpack.config.ts` 里添加如下配置: ```javascript const webpack = require(&#39;webpack&#39;); const isDevelopment = process.env.NODE_ENV !== &#39;production&#39;; module.exports = { // ...其他配置 mode: isDevelopment ? &#39;development&#39; : &#39;production&#39;, devServer: { hot: true, // 开启 HMR liveReload: false, }, plugins: [ isDevelopment && new webpack.HotModuleReplacementPlugin(), ].filter(Boolean), }; ``` 同时,在 React 项目里,可结合 `@pmmmwh/react-refresh-webpack-plugin` 来实现组件的热更新: ```javascript const ReactRefreshWebpackPlugin = require(&#39;@pmmmwh/react-refresh-webpack-plugin&#39;); const isDevelopment = process.env.NODE_ENV !== &#39;production&#39;; module.exports = { // ...其他配置 module: { rules: [ { test: /\.(js|jsx|ts|tsx)$/, exclude: /node_modules/, use: [ { loader: &#39;babel-loader&#39;, options: { plugins: [isDevelopment && require.resolve(&#39;react-refresh/babel&#39;)].filter(Boolean), }, }, ], }, ], }, plugins: [ isDevelopment && new ReactRefreshWebpackPlugin(), ].filter(Boolean), }; ``` ### 解决 ts-loader 选项错误问题 ts-loader 选项错误通常是由于配置不正确导致的。确保 `webpack.config.js` 中的 `ts-loader` 配置正确: ```javascript module.exports = { // ...其他配置 module: { rules: [ { test: /\.tsx?$/, use: [ { loader: &#39;ts-loader&#39;, options: { transpileOnly: true, // 可选,提高编译速度 happyPackMode: true, // 可选,配合 HappyPack 提高性能 }, }, ], exclude: /node_modules/, }, ], }, resolve: { extensions: [&#39;.tsx&#39;, &#39;.ts&#39;, &#39;.js&#39;], }, }; ``` 同时,要保证 `tsconfig.json` 文件的配置没有问题,尤其是 `compilerOptions` 部分。 ### 解决 Webpack 无法解析模块问题 Webpack 无法解析模块可能是因为缺少相应的解析规则或者模块路径配置有误。可以通过以下方法解决- **配置解析规则**:在 `webpack.config.js` 中添加解析规则: ```javascript module.exports = { // ...其他配置 resolve: { extensions: [&#39;.tsx&#39;, &#39;.ts&#39;, &#39;.js&#39;, &#39;.json&#39;], // 解析文件扩展名 modules: [&#39;node_modules&#39;, path.resolve(__dirname, &#39;src&#39;)], // 解析模块的目录 alias: { // 配置模块别名 &#39;@&#39;: path.resolve(__dirname, &#39;src&#39;), }, }, }; ``` - **检查模块安装**:确保所有依赖都已正确安装,可以通过 `npm install` 或 `yarn install` 来安装依赖。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值