38

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",
R. Richards
  • 24,603
  • 10
  • 64
  • 64
Jorge Nava
  • 495
  • 1
  • 5
  • 10

8 Answers8

23

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.

Hamza
  • 171
  • 1
  • 10
Sidouxie
  • 347
  • 2
  • 6
  • Did not work for me. I get `'REACT_APP_MY_ENV_VAR' is not defined.` in the `next.conf.js` file. What calls `next.conf.js`, how do I know it's being used? – Paul Aug 16 '21 at 22:48
  • 2
    Worked for me except, I had to use colon instead of = env: { YB_RAILS: process.env.YB_RAILS, }, – PhilCowan Dec 14 '21 at 20:26
  • In NEXT JS, there is a built-in support for loading variables actually, You dont need to specifically find a loader –  Jan 21 '22 at 09:51
  • 1
    Thank you sir, been at this the whole day ! :D Genius ;) – heyooo12 Mar 04 '22 at 13:54
13

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 configured the webpack.config.js as it is written in docs.

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

Brad Ahrens
  • 4,864
  • 5
  • 36
  • 47
Jakub Siwiec
  • 428
  • 4
  • 13
10

If you encounter this error with a create-react-app app, you need to go to the react-scripts webpack config.

In your webpack.config.js under 'resolve':

resolve: {
  // ...
  // add the fallback setting below 
  fallback: {
    "fs": false,
    "os": false,
    "path": false
  },
  // ...
}
user16217248
  • 3,119
  • 19
  • 19
  • 37
E G
  • 498
  • 6
  • 7
  • 2
    Editing node_modules wouldn't be the right solution if you're collaborating with others. They would still face this error on every fresh npm install – Nihal Dias Mar 07 '23 at 13:11
6

I faced a similar problem. dotenv-webpack installation will suffice. It is the dotenv require statement which is causing problem. Remove that and you will be good to go!

require('dotenv').config(); // Remove this line
dwb
  • 2,136
  • 13
  • 27
aayush612
  • 83
  • 2
  • 7
1

I solved a similar problem for myself as follows:

  1. create a file next.config.js in root directory
  2. which contains the following code:

module.exports = {
  env: {
    REACT_APP_API_HOST:'http://localhost:5001',
  },
}
  1. and after that, the variable REACT_APP_API_HOST became available in client part of my Next.js application as process.env.REACT_APP_API_HOST
MaxsaDev
  • 11
  • 1
0

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

...
"browser": {
  "fs": false
},
...
Daniel Danielecki
  • 8,508
  • 6
  • 68
  • 94
0

I want to also add TypeScript code on top of @Sidouxie's solution:

  1. Create a file that ends with .d.ts in your root directory.

  2. Add this to the file:

    declare global {
     namespace NodeJS {
       interface ProcessEnv {
         API_URL:string;
       }
      }
    }
    

This will globally check your environment variables in your TypeScript project.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Ömer Ayhan
  • 73
  • 1
  • 4
0

I was having the same problem, this was the solution for me enter link description here

Will Diaz
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 25 '23 at 22:47