Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to set environment variables from within package.json?

How to set some environment variables from within package.json to be used with npm start like commands?

This is what I presently have in my package.json:

{
...
"scripts": {
"help": "tagove help",
"start": "tagove start"
}
...


I need to set environment variables (like NODE_ENV) in the beginning script while as yet having the option to begin the application with only one order, npm start.
by

3 Answers

akshay1995
Set the environment variable in the script command:

...
"scripts": {
"start": "node app.js",
"test": "NODE_ENV=test mocha --reporter spec"
},
...

Then use process.env.NODE_ENV in your app.

Note: This is for Mac & Linux only. For Windows refer to the comments.
RoliMishra
From documentation:

{
"scripts": {
"build": "cross-env NODE_ENV=production OTHERFLAG=myValue webpack --config build/webpack.config.js"
}
}

Notice that if you want to set multiple global vars, you just state them in succession, followed by your command to be executed.

Ultimately, the command that is executed (using spawn) is:

webpack --config build/webpack.config.js

The NODE_ENV environment variable will be set by cross-env
pankajshivnani123
Try this on Windows by replacing YOURENV:

{
...
"scripts": {
"help": "set NODE_ENV=YOURENV && tagove help",
"start": "set NODE_ENV=YOURENV && tagove start"
}
...
}

Login / Signup to Answer the Question.