dev.mraj dev.mraj - 6 months ago 100
Javascript Question

How to set Environment variables from within package.json [Node.js]

How to set some Environment variables from within package.json to be used with

npm start
like commands

here is what i want to achieve.

package.json

{
...
"scripts": {
"help": "actionhero help",
"start": "actionhero start",
"startCluster": "actionhero startCluster --workers=1",
"actionhero": "actionhero"
}
...
}


here i want to set Environment variables (like NODE_ENV and others) in start script section, and i want to start app with just one command,
npm start
.

Answer

Set the env 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.

Comments