英文:
webpack < 5 does not include polyfills for node.js core modules
问题
我运行了一个简单的create-react-app项目来测试JWT身份验证。我为RESTful API创建了一个Node服务器,另外为create-react-app创建了另一个Node服务器。API的服务器正常运行,但是create-react-app的服务器在出现一长串错误时崩溃,错误如下所示:
(接下来是一长串错误信息,包括有关模块未找到、Webpack升级相关的警告以及与核心Node.js模块的错误)
我已经尝试了以下步骤,如以下论坛中所示:
- Stack Overflow: Module not found: Error: Can't resolve 'fs'
- GitHub Issue: react-pdf - Module not found: Error: Can't resolve 'fs'
- Stack Overflow: How to polyfill Node core modules in Webpack 5
这意味着我尝试了降级React版本、重新安装依赖项,并且尝试修改webpack.config.js文件以添加所需的内容,但问题似乎仍然存在。
我应该怎么做?
谢谢您的时间!
英文:
I m running a simple create-react-app project to test out JWT Authentication. I have made a node server for both the RESTFull API and another node server for create-react-app. The server for the API is runninng just fine. However, the server for create-react-app keeps crashing with a long list of errors as such:
Failed to compile.
Module not found: Error: Can't resolve 'fs' in '/Users/reevefernandes/node_modules/chokidar'
WARNING in ../../../node_modules/minimatch/minimatch.js 5:11-26
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/minimatch'
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 }
WARNING in ../../../node_modules/nodemon/lib/config/exec.js 20:14-63
Critical dependency: the request of a dependency is an expression
WARNING in ../../../node_modules/nodemon/lib/version.js 16:12-54
Critical dependency: the request of a dependency is an expression
ERROR in ../../../node_modules/chokidar/index.js 6:11-24
Module not found: Error: Can't resolve 'fs' in '/Users/reevefernandes/node_modules/chokidar'
ERROR in ../../../node_modules/chokidar/index.js 7:16-31
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/chokidar'
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 }
ERROR in ../../../node_modules/chokidar/lib/constants.js 5:4-19
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/chokidar/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 }
ERROR in ../../../node_modules/chokidar/lib/constants.js 9:11-24
Module not found: Error: Can't resolve 'os' in '/Users/reevefernandes/node_modules/chokidar/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 }
ERROR in ../../../node_modules/chokidar/lib/fsevents-handler.js 3:11-24
Module not found: Error: Can't resolve 'fs' in '/Users/reevefernandes/node_modules/chokidar/lib'
ERROR in ../../../node_modules/chokidar/lib/fsevents-handler.js 4:16-31
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/chokidar/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 }
ERROR in ../../../node_modules/chokidar/lib/nodefs-handler.js 3:11-24
Module not found: Error: Can't resolve 'fs' in '/Users/reevefernandes/node_modules/chokidar/lib'
ERROR in ../../../node_modules/chokidar/lib/nodefs-handler.js 4:16-31
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/chokidar/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 }
ERROR in ../../../node_modules/glob-parent/index.js 4:23-52
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/glob-parent'
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 }
ERROR in ../../../node_modules/glob-parent/index.js 5:14-36
Module not found: Error: Can't resolve 'os' in '/Users/reevefernandes/node_modules/glob-parent'
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 }
ERROR in ../../../node_modules/is-binary-path/index.js 3:13-28
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/is-binary-path'
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 }
ERROR in ../../../node_modules/nodemon/lib/cli/parse.js 18:9-22
Module not found: Error: Can't resolve 'fs' in '/Users/reevefernandes/node_modules/nodemon/lib/cli'
ERROR in ../../../node_modules/nodemon/lib/cli/parse.js 19:11-26
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/nodemon/lib/cli'
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 }
ERROR in ../../../node_modules/nodemon/lib/config/exec.js 1:13-28
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/nodemon/lib/config'
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 }
ERROR in ../../../node_modules/nodemon/lib/config/exec.js 2:11-24
Module not found: Error: Can't resolve 'fs' in '/Users/reevefernandes/node_modules/nodemon/lib/config'
ERROR in ../../../node_modules/nodemon/lib/config/load.js 2:9-22
Module not found: Error: Can't resolve 'fs' in '/Users/reevefernandes/node_modules/nodemon/lib/config'
ERROR in ../../../node_modules/nodemon/lib/config/load.js 3:11-26
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/nodemon/lib/config'
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 }
ERROR in ../../../node_modules/nodemon/lib/help/index.js 1:9-22
Module not found: Error: Can't resolve 'fs' in '/Users/reevefernandes/node_modules/nodemon/lib/help'
ERROR in ../../../node_modules/nodemon/lib/help/index.js 2:11-26
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/nodemon/lib/help'
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 }
ERROR in ../../../node_modules/nodemon/lib/monitor/match.js 2:13-28
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/nodemon/lib/monitor'
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 }
ERROR in ../../../node_modules/nodemon/lib/monitor/match.js 3:11-24
Module not found: Error: Can't resolve 'fs' in '/Users/reevefernandes/node_modules/nodemon/lib/monitor'
ERROR in ../../../node_modules/nodemon/lib/monitor/run.js 2:17-39
Module not found: Error: Can't resolve 'fs' in '/Users/reevefernandes/node_modules/nodemon/lib/monitor'
ERROR in ../../../node_modules/nodemon/lib/monitor/run.js 5:19-43
Module not found: Error: Can't resolve 'child_process' in '/Users/reevefernandes/node_modules/nodemon/lib/monitor'
ERROR in ../../../node_modules/nodemon/lib/monitor/run.js 17:11-26
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/nodemon/lib/monitor'
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 }
ERROR in ../../../node_modules/nodemon/lib/monitor/run.js 19:27-48
Module not found: Error: Can't resolve 'os' in '/Users/reevefernandes/node_modules/nodemon/lib/monitor'
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 }
ERROR in ../../../node_modules/nodemon/lib/monitor/watch.js 8:11-26
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/nodemon/lib/monitor'
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 }
ERROR in ../../../node_modules/nodemon/lib/nodemon.js 2:11-26
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/nodemon/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 }
ERROR in ../../../node_modules/nodemon/lib/rules/parse.js 3:9-22
Module not found: Error: Can't resolve 'fs' in '/Users/reevefernandes/node_modules/nodemon/lib/rules'
ERROR in ../../../node_modules/nodemon/lib/spawn.js 1:13-28
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/nodemon/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 }
ERROR in ../../../node_modules/nodemon/lib/spawn.js 5:14-44
Module not found: Error: Can't resolve 'child_process' in '/Users/reevefernandes/node_modules/nodemon/lib'
ERROR in ../../../node_modules/nodemon/lib/utils/index.js 2:11-26
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/nodemon/lib/utils'
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 }
ERROR in ../../../node_modules/nodemon/lib/version.js 3:9-22
Module not found: Error: Can't resolve 'fs' in '/Users/reevefernandes/node_modules/nodemon/lib'
ERROR in ../../../node_modules/nodemon/lib/version.js 4:11-26
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/nodemon/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 }
ERROR in ../../../node_modules/nodemon/lib/version.js 5:11-40
Module not found: Error: Can't resolve 'child_process' in '/Users/reevefernandes/node_modules/nodemon/lib'
ERROR in ../../../node_modules/picomatch/lib/constants.js 3:13-28
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/picomatch/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 }
ERROR in ../../../node_modules/picomatch/lib/picomatch.js 3:13-28
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/picomatch/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 }
ERROR in ../../../node_modules/picomatch/lib/utils.js 3:13-28
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/picomatch/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 }
ERROR in ../../../node_modules/pstree.remy/lib/index.js 1:13-42
Module not found: Error: Can't resolve 'child_process' in '/Users/reevefernandes/node_modules/pstree.remy/lib'
ERROR in ../../../node_modules/pstree.remy/lib/tree.js 1:14-44
Module not found: Error: Can't resolve 'child_process' in '/Users/reevefernandes/node_modules/pstree.remy/lib'
ERROR in ../../../node_modules/pstree.remy/lib/utils.js 1:14-44
Module not found: Error: Can't resolve 'child_process' in '/Users/reevefernandes/node_modules/pstree.remy/lib'
ERROR in ../../../node_modules/readdirp/index.js 3:11-24
Module not found: Error: Can't resolve 'fs' in '/Users/reevefernandes/node_modules/readdirp'
ERROR in ../../../node_modules/readdirp/index.js 6:4-21
Module not found: Error: Can't resolve 'stream' in '/Users/reevefernandes/node_modules/readdirp'
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 }
ERROR in ../../../node_modules/readdirp/index.js 7:16-31
Module not found: Error: Can't resolve 'path' in '/Users/reevefernandes/node_modules/readdirp'
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 }
ERROR in [eslint] EACCES: permission denied, mkdir '/Users/reevefernandes/Desktop/user-jwt/client/node_modules/.cache'
webpack compiled with 44 errors and 3 warnings
One of your dependencies, babel-preset-react-app, is importing the
"@babel/plugin-proposal-private-property-in-object" package without
declaring it in its dependencies. This is currently working because
"@babel/plugin-proposal-private-property-in-object" is already in your
node_modules folder for unrelated reasons, but it may break at any time.
babel-preset-react-app is part of the create-react-app project, which
is not maintianed anymore. It is thus unlikely that this bug will
ever be fixed. Add "@babel/plugin-proposal-private-property-in-object" to
your devDependencies to work around this error. This will make this message
go away.
I went thorugh the following steps as shown in these forums:
- https://stackoverflow.com/questions/57161839/module-not-found-error-cant-resolve-fs-in
- https://github.com/diegomura/react-pdf/issues/1670
- https://stackoverflow.com/questions/64557638/how-to-polyfill-node-core-modules-in-webpack-5
Which means, I tried to downgrade the react version and reintsall dependencies and also tapped into the webpack.config.js file to add the required, the problem still seems to persist.
What must I do?
Thank you for your time!
答案1
得分: 1
我有一个React.js和Firebase应用程序,它出现了相同的错误。我通过移除require('dotenv').config();
来解决了我的问题。新的React.js版本会自动处理将dotenv
包加载到文件中。所以,如果你有这行代码,它会导致这个polyfill错误。
这个polyfill错误不仅仅出现在dotenv
包上,还可能出现在你安装的其他一些包上。
英文:
I had a React.js and Firebase app and it was giving the same error. I solved my issue by removing require('dotenv').config();
. New React.js versions automatically handles loading dotenv
package into the files. So, if you have this line, it will give this polyfill error.
This polyfill error can be seen not just with dotenv
package but also with some other packages you have installed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论