12
votes

I'm trying to use dotenv with an angle, but when it comes to requiring dotenv

 require('dotenv').config()  
 or
 const Dotenv = require('dotenv-webpack');

I get the following error:

ERROR in ./node_modules/dotenv/lib/main.js Module not found: Error: Can't resolve 'fs' in 'C:\Users\57322\Favorites\Proyecto\core4edu\node_modules\dotenv\lib'

package.json

"dotenv": "^8.2.0",
"dotenv-webpack": "^1.7.0",
3
Have you checked this answer? github.com/motdotla/dotenv/issues/233Roy
yes, I tried that and it doesn't workJorge Nava

3 Answers

4
votes

I think the problem is related to webpack, I had the same problem on a next js project and here’s 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 will solve your problem.

1
votes

I had a similar situation - working in the webpack development mode, installing dotenv, getting an error "Module not found: Error: Can't resolve 'fs' in node_modules\dotenv\lib'".

I uninstalled dotenv and stayed just with dotenv-webpack for development. I configurated the webpack.config.js as it is written in docs.

Everything works now. Env variables are accessible globally without creating any additional file.

-1
votes

This little addition to package.json starting from Angular 9 (confirmed on Angular 10) fixes this problem.

...
"browser": {
  "fs": false
},
...