Open damozhang opened 2 years ago
great. It's working for me.
I somehow fixed it too using
react-app-rewired
Make sure you install this package and create new fileconfig-overrides.js
in your root directory/* config-overrides.js */ const webpack = require('webpack'); module.exports = function override(config, env) { //do stuff with the webpack config... config.resolve.fallback = { url: require.resolve('url'), 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; }
Now edit your
package.json
for makingreact-app-rewired
to work.//package.json ... "dependencies": { "@testing-library/jest-dom": "^5.16.1", "@testing-library/react": "^12.1.2", "@testing-library/user-event": "^13.5.0", "assert": "^2.0.0", "buffer": "^6.0.3", "crypto": "npm:crypto-browserify", "crypto-browserify": "^3.12.0", "https-browserify": "^1.0.0", "os-browserify": "^0.3.0", "react": "^17.0.2", "react-app-rewired": "^2.1.9", "react-dom": "^17.0.2", "react-scripts": "5.0.0", "rewire": "^6.0.0", "stream": "npm:stream-browserify", "stream-browserify": "^3.0.0", "stream-http": "^3.2.0", "url": "^0.11.0", "web-vitals": "^2.1.2", "web3": "^1.6.1" }, "scripts": { "start": "react-app-rewired start", "build": "react-app-rewired build", "test": "react-app-rewired test", "eject": "react-app-rewired eject" },
Hope this helps.
Thanks.
looks, good! Thank you!
I am using craco for CRA config overrides. Does anybody know how to configure it to fix this issue?
Hi there if you are using craco because of tailwind i suggest you to install tailwind v3 craco not required anymore in last version of tailwind
I am using craco for CRA config overrides. Does anybody know how to configure it to fix this issue?
Dear @smelamud,
I posted a start to finish example using CRA and craco overrides. It's specific to the project @react-pdf/renderer, but I think it will give you a sense of how to do overrides using craco. https://stackoverflow.com/a/70441023/2193573
Good luck!
I too am using craco, but not tailwind and need a solution. I have temporarily removed packages that will not build and therefore lost functionality.
There are likely a lot of libraries in the ecosystem that will fail to build due to this issue. This may result in the loss of some great works because their authors either do not have the time or knowhow to fix this.
Here is one example of a great package, lost to this issue: Clean-CSS
I followed all the steps above and i am still getting "Uncaught ReferenceError: Buffer is not defined" error... I have tried everything, i don't know what else to do
Just to pile on -- I cannot proceed with upgrading to 5.0 until this gets fixed. I'm not going to install react-app-rewired/eject and hack around the absence of expected behavior. Can we get a hard answer on whether this fix will be included in 5.0.1?
Also not using 5.0 if this does not get addressed.
Same issue here, got resolved by adding fallbacks to node_modules/react-scripts/config/webpack.config.js and installing the said packages, this seems a temporary solution though for CI/CD deployments to netlify/vercel this won't hold and build will break :thinking:
I am using JWT-Simple, I have to add
//webpack.config.js
resolve: {
fallback: {
crypto: require.resolve('crypto-browserify'),
stream: require.resolve("stream-browserify"),
buffer: require.resolve("buffer/")
},
}
But it did not work at all, there is still no solution?π€π€
@PrimaryOmega I think you need something like in the webpack.configure.plugins
section of my craco.config.js
, as well as the webpack
definition at the top:
const webpack = require("webpack");
module.exports = {
webpack: {
configure: {
resolve: {
fallback: {
process: require.resolve("process/browser"),
zlib: require.resolve("browserify-zlib"),
stream: require.resolve("stream-browserify"),
util: require.resolve("util"),
buffer: require.resolve("buffer"),
asset: require.resolve("assert"),
},
},
plugins: [
new webpack.ProvidePlugin({
Buffer: ["buffer", "Buffer"],
process: "process/browser",
}),
],
},
},
};
I got this webpack config from the package maintainer, and then adapted it for use in craco. I also had to install the additional packages, in this case process browserify-zlib stream-browserify util buffer assert
.
@michaelmarziani
I am using Tailwind CSS v3, I put it in the postcss.config.js
const webpack = require("webpack");
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
}, webpack: {
configure: {
resolve: {
fallback: {
process: require.resolve("process/browser"),
zlib: require.resolve("browserify-zlib"),
stream: require.resolve("stream-browserify"),
util: require.resolve("util"),
buffer: require.resolve("buffer"),
asset: require.resolve("assert"),
},
},
plugins: [
new webpack.ProvidePlugin({
Buffer: ["buffer", "Buffer"],
process: "process/browser",
}),
],
},
},
}
And the dependencies you mentioned in the webpack.config.js
resolve: {
fallback: {
crypto: require.resolve('crypto-browserify'),
stream: require.resolve("stream-browserify"),
process: require.resolve("process/browser"),
zlib: require.resolve("browserify-zlib"),
util: require.resolve("util"),
buffer: require.resolve("buffer"),
asset: require.resolve("assert"),
},
}
and it didn't work π€§π€§
Does someone know how to install the previous version of CRA? npx create-react-app@4.0.3 my-app fails:
You are running `create-react-app` 4.0.3, which is behind the latest release (5.0.0).
We no longer support global installation of Create React App.
Please remove any global installs with one of the following commands:
- npm uninstall -g create-react-app
- yarn global remove create-react-app
npx create-react-app@4.0.3 failing for me as well, did clone an existing 4.0.3 project for now π
@michaelmarziani
I am using Tailwind CSS v3, I put it in the postcss.config.js
const webpack = require("webpack"); module.exports = { plugins: { tailwindcss: {}, autoprefixer: {}, }, webpack: { configure: { resolve: { fallback: { process: require.resolve("process/browser"), zlib: require.resolve("browserify-zlib"), stream: require.resolve("stream-browserify"), util: require.resolve("util"), buffer: require.resolve("buffer"), asset: require.resolve("assert"), }, }, plugins: [ new webpack.ProvidePlugin({ Buffer: ["buffer", "Buffer"], process: "process/browser", }), ], }, }, }
And the dependencies you mentioned in the webpack.config.js
resolve: { fallback: { crypto: require.resolve('crypto-browserify'), stream: require.resolve("stream-browserify"), process: require.resolve("process/browser"), zlib: require.resolve("browserify-zlib"), util: require.resolve("util"), buffer: require.resolve("buffer"), asset: require.resolve("assert"), }, }
and it didn't work π€§π€§
Bro try this am using tailwind v3 postcss 8 and it's works for me https://github.com/facebook/create-react-app/issues/11756#issuecomment-1001162736
@PrimaryOmega My configuration is specific to using craco to override CRA and inject additional webpack
configs.
@michaelmarziani I am using Tailwind CSS v3, I put it in the postcss.config.js
const webpack = require("webpack"); module.exports = { plugins: { tailwindcss: {}, autoprefixer: {}, }, webpack: { configure: { resolve: { fallback: { process: require.resolve("process/browser"), zlib: require.resolve("browserify-zlib"), stream: require.resolve("stream-browserify"), util: require.resolve("util"), buffer: require.resolve("buffer"), asset: require.resolve("assert"), }, }, plugins: [ new webpack.ProvidePlugin({ Buffer: ["buffer", "Buffer"], process: "process/browser", }), ], }, }, }
And the dependencies you mentioned in the webpack.config.js
resolve: { fallback: { crypto: require.resolve('crypto-browserify'), stream: require.resolve("stream-browserify"), process: require.resolve("process/browser"), zlib: require.resolve("browserify-zlib"), util: require.resolve("util"), buffer: require.resolve("buffer"), asset: require.resolve("assert"), }, }
and it didn't work π€§π€§
Bro try this am using tailwind v3 postcss 8 and it's works for me #11756 (comment) It worked, but it does not show the errors as before by the system console or in the browser, only in the developer console. πͺπͺIt is programmable, but now I have to visit the developer console more often hahaha. π€£π€£
For those that don't want to use craco
or react-app-rewired
. You can use patch-package
.
Install patch-package: yarn add patch-package
.
Install the needed pollyfills and modify node_modules/react-scripts/config/webpack.config.js
. Here's an example. This is taken from Webpack's docs. Don't add all of them, only the ones you need. Side note, for those wondering which ones need to be pollyfilled, do an initial build of your application and it will tell you. Another side note, make sure you install the packages first before modifying the webpack config.
Run yarn patch-package react-scripts
. This will generate a patch (this should be committed in your repo going forward).
Add a postinstall script to package.json: "postinstall": "yarn patch-package",
. Now, anytime, someone installs npm deps on this project, will get the patch you created in step 3 applied automatically.
Enjoy zero compile errors.
I have same issue when I try to use web3.js (1.6.1)
Compiled with problems:X
ERROR in ./node_modules/cipher-base/index.js 3:16-43
Module not found: Error: Can't resolve 'stream' in '/home/ilijapet/proba/smartCOOP2.0/smartcoop_react/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:
ERROR in ./node_modules/eth-lib/lib/bytes.js 9:193-227
Module not found: Error: Can't resolve 'crypto' in '/home/ilijapet/proba/smartCOOP2.0/smartcoop_react/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:
ERROR in ./node_modules/ethereumjs-util/dist.browser/account.js 71:31-48
Module not found: Error: Can't resolve 'assert' in '/home/ilijapet/proba/smartCOOP2.0/smartcoop_react/node_modules/ethereumjs-util/dist.browser'
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:
ERROR in ./node_modules/ethereumjs-util/dist.browser/address.js 14:31-48
Module not found: Error: Can't resolve 'assert' in '/home/ilijapet/proba/smartCOOP2.0/smartcoop_react/node_modules/ethereumjs-util/dist.browser'
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:
ERROR in ./node_modules/ethereumjs-util/dist.browser/object.js 46:31-48
Module not found: Error: Can't resolve 'assert' in '/home/ilijapet/proba/smartCOOP2.0/smartcoop_react/node_modules/ethereumjs-util/dist.browser'
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:
ERROR in ./node_modules/web3-eth-accounts/lib/index.js 31:74-91
Module not found: Error: Can't resolve 'crypto' in '/home/ilijapet/proba/smartCOOP2.0/smartcoop_react/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:
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 '/home/ilijapet/proba/smartCOOP2.0/smartcoop_react/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:
ERROR in ./node_modules/web3-providers-http/lib/index.js 30:11-26
Module not found: Error: Can't resolve 'http' in '/home/ilijapet/proba/smartCOOP2.0/smartcoop_react/node_modules/web3-providers-http/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:
ERROR in ./node_modules/web3-providers-http/lib/index.js 32:12-28
Module not found: Error: Can't resolve 'https' in '/home/ilijapet/proba/smartCOOP2.0/smartcoop_react/node_modules/web3-providers-http/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:
ERROR in ./node_modules/web3-providers-ws/lib/helpers.js 11:12-26
Module not found: Error: Can't resolve 'url' in '/home/ilijapet/proba/smartCOOP2.0/smartcoop_react/node_modules/web3-providers-ws/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:
ERROR in ./node_modules/xhr2-cookies/dist/xml-http-request.js 37:11-26
Module not found: Error: Can't resolve 'http' in '/home/ilijapet/proba/smartCOOP2.0/smartcoop_react/node_modules/xhr2-cookies/dist'
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:
ERROR in ./node_modules/xhr2-cookies/dist/xml-http-request.js 39:12-28
Module not found: Error: Can't resolve 'https' in '/home/ilijapet/proba/smartCOOP2.0/smartcoop_react/node_modules/xhr2-cookies/dist'
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:
ERROR in ./node_modules/xhr2-cookies/dist/xml-http-request.js 41:9-22
Module not found: Error: Can't resolve 'os' in '/home/ilijapet/proba/smartCOOP2.0/smartcoop_react/node_modules/xhr2-cookies/dist'
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:
ERROR in ./node_modules/xhr2-cookies/dist/xml-http-request.js 43:10-24
Module not found: Error: Can't resolve 'url' in '/home/ilijapet/proba/smartCOOP2.0/smartcoop_react/node_modules/xhr2-cookies/dist'
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:
Works for me! Solution down below...
Hi Guys i'm using latest version
React => v17 React scripts=> v5 webpack => v5
To Fix The Problem
1) Install π
`
"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) Creating config-overrides.js
Create config-coverrides.js in Root dir like this π
3) Add configs to config-overrides.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; }
I.e π
3) Change packages.js
Hope this Help
Hi Guys i'm using latest version
React => v17 React scripts=> v5 webpack => v5
To Fix The Problem
1) Install
`
"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) Creating config-overrides.js
Create config-coverrides.js in Root dir like this point_down
3) Add configs to config-overrides.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; }
I.e point_down
3) Change packages.js
Hope this Help
Issue resolved . Thanks a lot @investor!
It's okay bro β€οΈ
The same issue, it's seen that 2 options are possible:
A)
B)
But after configuring webpack - there is a lot of dependencies to install etc.
npm i fs is not working @ilijapet & @BushraHussain looks like npm blocked it? now what?
I'm not going to use some react-app-rewired but I need "crypto" module to work. What's proven to work?
I'm not going to use some react-app-rewired but I need "crypto" module to work. What's proven to work?
It's not a solution for CRA, but I switched to next.js and this is not a problem.
Another victim here. In my case colors
requires os
and winston
logger requires os
, fs
, path
, zlib
, http
and https
which means the project obviously doesn't even compile.
Having scrolled through this issue I don't see any obvious solutions. Are there none apart from reverting back to v4 or have I missed anything (not going to install Rewire just to fix something that worked fine before really).
I got it working by ejecting and adding the resolve fallbacks to the config/webpack.config.js
@acidhax Ejecting is not an option!
Ejecting is worse than adding Rewire...
Create React App User Guide:
Step 1: npx create-react-app my-app
Step 2: npm eject
Same issue here with ethers js.
Module not found: Error: Can't resolve 'fs' in "node-env/file/path" Module not found: Error: Can't resolve 'path' in "node-env/file/path" Module not found: Error: Can't resolve 'os' in "node-env/file/path
Forced to use old version of react scripts, which in turn forces me to use an old version of tailwind :(
It's the same for me.
The latest version of TailwindCSS is v3. Its guide is here: https://tailwindcss.com/docs/guides/create-react-app However, it uses react-scripts 5.0.0. This breaks web3.js with polyfill. So we have to downgrade out TailwindCSS version
I followed this guide in order to downgrade to TailwindCSS v2 (I was on V3):
https://v2.tailwindcss.com/docs/guides/create-react-app
and then I changed react_scripts
in package.json
form 5.0.0
to 4.0.3
it still does not work out for me
it still does not work out for me
If you want open a Stakoverflow question, link it here and I try to answer on Stackoverflow in more details
Having this issue with zlib, stream, and util. Mostly for @react-pdf
I've used react-app-rewired many years ago (didnt give me any issues).
Is the consensus at this point:
eject
?
We tried to port all of our direct usages of Node over to npm
packages.
We were actually successful in moving our direct usages of "crypto"
to sha.js
and Buffer
to js-base64
, but then we hit a wall when we we realized we had dozens of dependencies that depend on safe-buffer
(46 references in package.json
), which itself internally uses Buffer
.
Have any of you tried this approach before deciding to eject or rewire? I have the impression that it would work for many of the cases presented.
The way I worked around this was to install the following npm libraries
"react-app-rewired": "^2.1.11"
(in package.json devDependencies)
"node-polyfill-webpack-plugin": "^1.1.4"
(in package.json dependencies)
update the npm scripts to use react-app-rewired instead of react-scripts, for example
"scripts": {
"build": "react-app-rewired build",
"start": "set WDS_SOCKET_PORT=0 && react-app-rewired start"
}
include a config-overrides.js file next to package.json, for example
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
module.exports = function override(config, env) {
config.plugins.push(new NodePolyfillPlugin({
excludeAliases: [
"assert",
"console",
"constants",
"crypto",
"domain",
"events",
"http",
"https",
"os",
"path",
"punycode",
"querystring",
"_stream_duplex",
"_stream_passthrough",
"_stream_transform",
"_stream_writable",
"string_decoder",
"sys",
"timers",
"tty",
"url",
"util",
"vm",
"zlib"
]
}));
return config;
};
I excluded a bunch of aliases (one or more may have been giving me a problem), but I guess you could try it first without exclusions and then exclude stuff as necessary, or exclude everything and then start deleting exclusions, however you want to approach it. See the "node-polyfill-webpack-plugin" documentation for details.
I also had problems with it not working at all and had to blast my node_modules and other distributables and reinstall, rebuild for it to then magically work.
Anyway, that was my experience and my app seems to be working now.
(I had started with trying to import various individual npm packages for individual fallbacks, as some people have mentioned on this thread, but I didn't have any luck going that route, (the fs package on npm seems bogus btw), and the node-polyfill-webpack-plugin seems a lot cleaner.)
I'm looking forward to a fix for this in the next verision for CRA so I can get rid of the above crud.
Having this issue with zlib, stream, and util. Mostly for
@react-pdf
I've used react-app-rewired many years ago (didnt give me any issues).
Is the consensus at this point:
- Use react-app-rewired , or
- eject
?
@acomito, I was able to make this work without too much fuss, specifically for @react-pdf/renderer using craco which I think seems better maintained than rewired.
Here's the step by step instructions I wrote for a new @react-pdf/renderer project on stackoverflow. Good luck!
Having this issue with web3 and web3-react.
I have the some problem, +buffer and stream modules not found. (these are core node packages)
I'm facing the same issue.. ERROR in ./node_modules/convert-source-map/node_modules/safe-buffer/index.js 2:13-30 Module not found: Error: Can't resolve 'buffer' in \node_modules\convert-source-map\node_modules\safe-buffer' @ ./node_modules/convert-source-map/index.js 4:17-39 @ ./node_modules/@babel/core/lib/transformation/normalize-file.js 49:15-44 @ ./node_modules/@babel/core/lib/transformation/index.js 24:21-48 @ ./node_modules/@babel/core/lib/transform.js 20:22-49 @ ./node_modules/@babel/core/lib/index.js 242:17-39
any help?
Same issue when I import the taquito
library.
I attempted to downgrade create-react-app
back to 4.0.3
, but it fails with the error We no longer support global installation of Create React App
any time I run it.
Is there a way I can bypass this error when using the old version of CRA? It's frustrating to be forced to deal with the polyfill issues.
I just downgraded react-scripts to v4.0.3 and it solved my problem with dotenv, but in a tiny project.
Downgrading to v4.0.3 fixed the issue for me as well using @walletconnect/web3-provider
I temporarily dealt with this by adding aliases to my dependencies. How bad is this solution?
Example:
"buffer": "^6.0.3", "crypto": "npm:crypto-browserify", "stream": "npm:stream-browserify",
Thanks a lot! It really helps to deal with v5 without eject.
I too have issues with this after upgrading. I'd be happy to not include any polyfills and follow the instructions in the error message, if I just knew how.. ?
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "stream": false }
My assumption is that I don't need polyfills if I only target newer browsers. Is there any way to fix this without ejecting or using rewire or even polyfills?
I'm receiving an error for the packages stream
zlib
and util
.
I too have issues with this after upgrading. I'd be happy to not include any polyfills and follow the instructions in the error message, if I just knew how.. ?
If you don't want to include a polyfill, you can use an empty module like this: resolve.fallback: { "stream": false }
My assumption is that I don't need polyfills if I only target newer browsers. Is there any way to fix this without ejecting or using rewire or even polyfills?
I'm receiving an error for the packages
stream
zlib
andutil
.
It's not so much an issue of running in newer browsers, it's more so because one of your dependencies (or a dependency's dependency, etc.) leverages a node.js (server-side) API in some form or fashion.
I think your best bet is craco or react-app-rewired.
The way I worked around this was to install the following npm libraries
"react-app-rewired": "^2.1.11"
(in package.json devDependencies)
"node-polyfill-webpack-plugin": "^1.1.4"
(in package.json dependencies)update the npm scripts to use react-app-rewired instead of react-scripts, for example
"scripts": { "build": "react-app-rewired build", "start": "set WDS_SOCKET_PORT=0 && react-app-rewired start" }
include a config-overrides.js file next to package.json, for example
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin"); module.exports = function override(config, env) { config.plugins.push(new NodePolyfillPlugin({ excludeAliases: [ "assert", "console", "constants", "crypto", "domain", "events", "http", "https", "os", "path", "punycode", "querystring", "_stream_duplex", "_stream_passthrough", "_stream_transform", "_stream_writable", "string_decoder", "sys", "timers", "tty", "url", "util", "vm", "zlib" ] })); return config; };
I excluded a bunch of aliases (one or more may have been giving me a problem), but I guess you could try it first without exclusions and then exclude stuff as necessary, or exclude everything and then start deleting exclusions, however you want to approach it. See the "node-polyfill-webpack-plugin" documentation for details.
I also had problems with it not working at all and had to blast my node_modules and other distributables and reinstall, rebuild for it to then magically work.
Anyway, that was my experience and my app seems to be working now.
(I had started with trying to import various individual npm packages for individual fallbacks, as some people have mentioned on this thread, but I didn't have any luck going that route, (the fs package on npm seems bogus btw), and the node-polyfill-webpack-plugin seems a lot cleaner.)
I'm looking forward to a fix for this in the next verision for CRA so I can get rid of the above crud.
This worked for me. Thank you!
Describe the bug
Compiled with problems
Did you try recovering your dependencies?
Which terms did you search for in User Guide?
react-scripts 5 webpack Module not found: Error: Can't resolve 'fs'
Environment
npx create-react-app --info
Environment Info:
Steps to reproduce
In a project with react-scripts v5.0.0
Expected behavior
Actual behavior
Some useful comments: