Home > Mobile >  Module not found: Can't resolve 'fs' in 'node_modules\dotenv\lib' - Nextj
Module not found: Can't resolve 'fs' in 'node_modules\dotenv\lib' - Nextj

Time:02-15

I am trying to use dotenv in React for API_URL. When I use it in index.js page in Next.js. I get this error

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

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

I am using dotenv by these methods:

1.

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

My package.json file is:

"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"
  }

CodePudding user response:

You should not use dotenv with Next.js. Here is guide how to use environment variables in Next.js.

  • Related