question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

v5 used to include polyfills for node.js core modules by default

See original GitHub issue

Describe the bug

Compiled with problems

Did you try recovering your dependencies?

yarn --version
1.22.15

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:

  current version of create-react-app: 5.0.0
  running from /Users/xxx/.config/yarn/global/node_modules/create-react-app

  System:
    OS: macOS 12.0.1
    CPU: (8) x64 Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
  Binaries:
    Node: 16.13.0 - /usr/local/Cellar/node@16/16.13.0/bin/node
    Yarn: 1.22.15 - /usr/local/bin/yarn
    npm: 8.1.0 - /usr/local/Cellar/node@16/16.13.0/bin/npm
  Browsers:
    Chrome: 96.0.4664.93
    Edge: Not Found
    Firefox: 94.0.1
    Safari: 15.1
  npmPackages:
    react:  17.0.2 
    react-dom:  17.0.2 
    react-scripts:  5.0.0 
  npmGlobalPackages:
    create-react-app: Not Found

Steps to reproduce

In a project with react-scripts v5.0.0

  1. yarn add -D dotenv
  2. yarn start

Expected behavior

Actual behavior

ERROR in ../../node_modules/dotenv/lib/main.js 24:11-24
Module not found: Error: Can't resolve 'fs' in '../node_modules/dotenv/lib'
 @ ./src/config.ts 5:0-28 8:0-13
 @ ./src/index.tsx 17:0-66 27:19-27 29:23-43 30:23-43 34:35-60

ERROR in ../../node_modules/dotenv/lib/main.js 26:13-28
Module not found: Error: Can't resolve 'path' in '../node_modules/dotenv/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: { "path": require.resolve("path-browserify") }'
	- install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
	resolve.fallback: { "path": false }
 @ ./src/config.ts 5:0-28 8:0-13
 @ ./src/index.tsx 17:0-66 27:19-27 29:23-43 30:23-43 34:35-60

ERROR in ../../node_modules/dotenv/lib/main.js 28:11-24
Module not found: Error: Can't resolve 'os' in '../node_modules/dotenv/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 }
 @ ./src/config.ts 5:0-28 8:0-13
 @ ./src/index.tsx 17:0-66 27:19-27 29:23-43 30:23-43 34:35-60

3 errors have detailed information that is not shown.
Use 'stats.errorDetails: true' resp. '--stats-error-details' to show it.

Some useful comments:

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:336
  • Comments:245 (10 by maintainers)

github_iconTop GitHub Comments

227reactions
raixcommented, Dec 15, 2021

My personal view: I personally consider it bad practice to include code in the front-end that was ment for back-end usage. From my experience: I’ve seen developers of any experienced try adding back-end npm packages on the front-end.

In Npm there is no clear difference to help out developers, it’s “Node Packages” not “Browser Packages”

Package maintainers should ideally build node or browser specific bundles.

Again this is my personal view I support the move in Webpack, I’ll of course bring it up in the maintainer group.

(Also see the release notes regarding webpack 5 - there’s a note regarding this breaking change for further details)

For now it would be helpful to collect examples of front-end packages suffering from this issue. (The examples would help us reevaluate the decision)

202reactions
dylantfcommented, Dec 17, 2021

“Then remove the package and find an alternative package better suited” is a great strategy when you have an unlimited budget, but in many cases these packages work just fine in the browser with a polyfill, and sometimes packages in the JS ecosystem go unmaintained and there aren’t any good alternatives. If Webpack 5 has an option to include the polyfills, but CRA does not, are we just expected to immediately eject upon upgrading to edit the webpack config, or just stay on CRA 4?

Read more comments on GitHub >

github_iconTop Results From Across the Web

i tried to Polyfill modules in webpack 5 but not working (Reactjs)
To use polyfill in webpack 5 in reactjs Follow the below steps: First Install npm install node-polyfill-webpack-plugin module.
Read more >
How to polyfill node core modules in webpack 5 - Alchemy
4 easy steps to fix polyfill node core modules in webpack 5 · 1. Install react-app-rewired · 2. Install missing dependencies · 3....
Read more >
webpack < 5 used to include polyfills for node.js core modules ...
js core modules by default. This is no longer the case. Verify if you need this module and configure a polyfill for it....
Read more >
webpack 5 used to include polyfills for node.js core ... - YouTube
Compiled with problems:ERROR in Module not found: Error: Can't resolve 'process' in BREAKING CHANGE: webpack less than 5 used to include ...
Read more >
Polyfill node core modules in Webpack 5 - The Coders Blog
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.This is no longer the case.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found