Module not found: Error: Can’t resolve ‘fs’ in node_modules\dotenv\lib’

I think the problem is related to webpack, I had the same problem on a next.js project and here is how I did to fix the problem.

Create file conf.js in root folder

I created a next.conf.js file in my root folder where there is my .env file, I imported dotenv and I exported the module where there is all my environment and these variables as if below.

export module dot env

Finally in my index file and my components I don’t need to import dotenv, I just need to paste my process.env.API_URL.

you dont need to import dotenv in index

I hope this example solves your problem.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)