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.
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.
suddenly i found that actionhero is using following code, that solved my problem by just passing --NODE_ENV=production in start script command option.
if(argv['NODE_ENV'] != null){
api.env = argv['NODE_ENV'];
} else if(process.env.NODE_ENV != null){
api.env = process.env.NODE_ENV;
}
i would really appreciate to accept answer of someone else who know more better way to set environment variables in package.json or init script or something like, where app bootstrapped by someone else.
I came across this today morning. I think this would serve the purpose
In a nut shell, the above library works as below
So in your package.json file, you just have to set just one single environment variable
You should not set ENV variables in package.json. actionhero uses NODE_ENV to allow you to change configuration options which are loaded from the files in ./config. Check out the redis config file, and see how NODE_ENV is uses to change database options in NODE_ENV=test
If you want to use other ENV variables to set things (perhaps the HTTP port), you still don't need to change anything in package.json. For example, if you set PORT=1234 in ENV and want to use that as the HTTP port in NODE_ENV=production, just reference that in the relevant config file, IE:
# in config/servers/web.js
exports.production = {
servers: {
web: function(api){
return {
port: process.env.PORT
}
}
}
}