Search code examples
node.jsexpressenvironment-variablesdotenv

How to fix No env file present for the current environment?


.env.development

secret_key=abcd

.env.production

secret_key=abcde

package.json

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "SET NODE_ENV=development & node server.js",
"prod": "SET NODE_ENV=production & node server.js",
"lint": "eslint",
"start": "node server.js"

},

in main file

require('custom-env').env(process.env.NODE_ENV);

But when i try

npm run dev or npm run prod

It shows No env file present for the current environment:production/development


Solution

  • Here if you want to get a particular environment.

    Create a .env.development file in your app's root directory

    require('custom-env').env('development')
    

    Inside package.json should be like this.

    "scripts": {
        "dev": "SET NODE_ENV=development&& node server.js"
      }