【问题标题】:dotenv module not working like before in a react appdotenv 模块在反应应用程序中不像以前那样工作
【发布时间】:2022-08-19 02:26:55
【问题描述】:

我创建了一个新的 react 项目,然后添加了 dotenv。

require(\'dotenv\').config();

但我得到了这个:

{{{

ERROR in ./node_modules/dotenv/lib/main.js 1:11-24

Module not found: Error: Can\'t resolve \'fs\' in \'C:\\...\\project\\node_modules\\dotenv\\lib\'


ERROR in ./node_modules/dotenv/lib/main.js 3:13-28

Module not found: Error: Can\'t resolve \'path\' in \'C:\\...\\project\\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 }


ERROR in ./node_modules/dotenv/lib/main.js 5:11-24

Module not found: Error: Can\'t resolve \'os\' in \'C:\\...\\project\\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 }
}}}

有人可以帮忙吗?

  • 我遇到了同样的错误,但无法解决。如果你得到它,请告诉我。
  • 这里同样的错误。看着这个话题。
  • 你试试 install install dotenv npm module link
  • 我安装了 dotenv

标签: node.js reactjs dotenv


【解决方案1】:

步骤1

安装 path-browserify、os-browserify 和 react-app-rewired

npm install path-browserify os-browserify react-app-rewired

第2步

在您的项目根目录中,创建 config-overrides.js 文件。

在这个文件中,放这个。

module.exports = function override (config, env) {
    console.log('override')
    let loaders = config.resolve
    loaders.fallback = {
        // existing configs...
        "fs": false,
        "os": require.resolve("os-browserify/browser"),
        "path": require.resolve("path-browserify"),
   }
    
    return config
}

第 3 步

重要的 !

将 package.json 中的 "start": react-scripts start 更改为 "start": react-app-rewired start

现在,重新启动您的项目。

npm start

【讨论】:

  • 好的谢谢。我会试试这个。但为什么事情发生了变化?我不习惯这样做来读取环境变量
  • 你试过了吗 ?之前,react 是自动完成的。自动支持 Node.js 内置模块,dotenv 需要 path 和 os 是 node.js 模块。但是现在在新版本的 react 中,如果你想使用 node.js 内置模块,你仍然需要做这个配置
【解决方案2】:

Ridoine12,我做了这些步骤,但它显示一个新的错误。

Module not found: Error: You attempted to import /home/everton/test/node_modules/path-browserify/index.js which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
You can either move it inside src/, or add a symlink to it from project's node_modules/.

你能帮我吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多