Search code examples
node.jsfsdotenv

Module not found: Error: Can't resolve 'fs' in dotenv/lib


All of a sudden, when creating a react production build, I get this error.

> [email protected] build
> react-scripts build

Creating an optimized production build...
Failed to compile.

Module not found: Error: Can't resolve 'fs' in '/workspace/safe-courier/client/node_modules/dotenv/lib'

I have searched on the web, found similar cases but different frameworks of which all were not of help in regards to this issue.

I have tried to uninstall dotenv and reinstall it again but i get the same error. I'm not sure what could be the problem understanding that fs module is part of nodejs and comes bundled with it


Solution

  • To solve this:

    1. Create the .env file at the root level of you app
    2. Name your variables beginning with REACT_APP_ // that's the key !!
    3. Restart your server with npm start each time you change an env variable
    4. Use your variable with process.env.NAMEOFYOURVARIABLE

    No need of dotenv for your React app.