I have a package.json with following (simplified) content in the scripts key:
... scripts: { "start": "NODE_ENV=${NODE_ENV:=production} node start-app.js", "poststart": "echo $NODE_ENV" } ...
From the command line I can run:
npm start
This will run my start-app.js script and set the process.env.NODE_ENV environment variable to "production". See here for syntax explanation.
The poststart will automatically run after start as described here.
However poststart will not "inherit" the NODE_ENV shell environment variable, so the echo command will not echo anything.
My producation code is a little more complex, but what I am trying to accomplish is passing down the NODE_ENV variable from the "starting point" to dependent scripts. Any suggestions/best practices on how to do that?
I dont want to hardcode the NODE_ENV in the poststart, because I might want to do:
NODE_ENV=development npm start
and I want everyting "down the chain" inherit the same environment.
For a test you can see the env variables by running npm run env-linux or npm run env-windows , and test that they make it into your app by running npm run start-linux or npm run start-windows .
"Edit the System environment variables" option will be popped in the result. Open that, select the "Path" and click on edit, then click "New" add your nodeJS Bin path i.e in my machine its installed in c:\programfiles\nodejs\node_modules\npm\bin. Once you added click "Ok" then close.
You have a few options:
env
for each command separatelypoststart
script, you can concatenate commands for npm like so: "start": "NODE_ENV=${NODE_ENV:=production} node start-app.js && echo $NODE_ENV"
NODE_ENV
. At our company, we successfully run all of our apps in different environments with pm2 (all the while having the same start command)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With