skip to Main Content

I am trying to set my environment variable within package.json so that I can use it within my server.js file. I have set it as NODE_ENV=development, set NODE_ENV=development,
cross-env NODE_ENV=development, export NODE_ENV=development but it is still returning undefined. I am not sure what is going wrong but I would love any insight into this problem!

package.json

"scripts": {
  "dev": "NODE_ENV=development npm run client & npm run start & npm run json-server & redis-server"
}
    

server.js

//returns undefined
console.log(process.env.NODE_ENV)

2

Answers


  1. I am not entirely sure how your file is structure but it seems you got
    some of the command wrong.

    You should use set NODE_ENV=development and you should double
    ampersand

    Honestly it much easier to define it in your main server file like
    this:

    const env = process.env.NODE_ENV || "development"
    
    Login or Signup to reply.
  2. Did you install and require the dotenv package and did you call the .config() function of it in your index.js ? This will probably help.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search