ahmed abid
ahmed abid

Reputation: 25

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

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

Upvotes: 0

Views: 1565

Answers (1)

rovaniemi
rovaniemi

Reputation: 97

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

Upvotes: 1

Related Questions