创建-反应-应用程序反应脚本的 Dotenv 问题/错误



我在反应应用程序中不断收到此错误

Compiled with problems: X
ERROR in ./ node_modules / dotenv / lib / main.js 1: 11 - 24
Module not found: Error: Can't resolve 'fs' in 'C: UsersUSERDesktopdappsneftynode_modulesdotenvlib'

ERROR in ./ node_modules / dotenv / lib / main.js 3: 13 - 28
Module not found: Error: Can't resolve 'path' in 'C: UsersUSERDesktopdappsneftynode_modulesdotenvlib'
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 / dotenv / lib / main.js 5: 11 - 24
Module not found: Error: Can't resolve 'os' in 'C: UsersUSERDesktopdappsneftynode_modulesdotenvlib'
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.config 提供的说明进行操作.js并添加回退,如下所示

module.exports = {
resolve: {
fallback: { "path": require.resolve("path-browserify")  },
},
}

没有区别。我在他们的 github 页面上检查了类似的问题,但找不到有效的修复程序(大多数是强调问题的评论,甚至没有指向合理的工作解决方案)

多亏了@user3133,我才能理解真正的问题。似乎dotenv应该只用于像nodejs这样的后端应用程序,而不是像react这样的前端应用程序。

当错误出现时,它并没有指出错误的来源,所以我基本上"迷失在暴风雪中"。我能够弄清楚错误来自在src目录中引用dotenv,该目录是react的域,该域在海外所有反应代码。

所以基本上我能够通过删除 src 目录中引用dotenv的所有代码行来解决这个问题,例如:

require('dotenv').config();

请注意,可以将其放在 src 目录之外的任何其他位置。

有趣的事实:react 有自己的 dotenv,您只需将REACT_APP_放在 env 变量前面即可访问它。

在这里阅读更多: 是否可以在 react 项目中使用 dotenv?

最新更新