【问题标题】:Module not found: Can't resolve 'fs' in 'node_modules\dotenv\lib' - Nextjs未找到模块:无法解析 'node_modules\dotenv\lib' 中的 'fs' - Nextjs
【发布时间】:2022-02-14 17:11:43
【问题描述】:

我正在尝试在 React 中为 API_URL 使用 dotenv。当我在 Next.js 的 index.js 页面中使用它时。我收到此错误

Module not found: Can't resolve 'fs' in 'node_modules\dotenv\lib'

Import trace for requested module:
./pages/index.js

我正在通过这些方法使用 dotenv:

1.

import { config } from 'dotenv';
config();
import dotenv from 'dotenv';
dotenv.config();
const dotenv = require('dotenv');
dotenv.config();

我的 package.json 文件是:

"dependencies": {
    "dayjs": "^1.10.7",
    "dotenv": "^16.0.0",
    "mongoose": "^6.2.1",
    "next": "12.0.10",
    "react": "17.0.2",
    "react-dom": "17.0.2"
  }

【问题讨论】:

    标签: reactjs typescript next.js


    【解决方案1】:

    您不应将 dotenv 与 Next.js 一起使用。 Here is guide如何在Next.js中使用环境变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 2021-08-01
      • 2021-02-21
      • 2022-11-12
      • 2021-01-30
      • 2022-08-05
      • 1970-01-01
      相关资源
      最近更新 更多