How to set some environment variables from within package.json
to be used with npm start
like commands?
Here's what I currently have in my package.json
:
{ ... "scripts": { "help": "tagove help", "start": "tagove start" } ... }
I want to set environment variables (like NODE_ENV
) in the start script while still being able to start the app with just one command, npm start
.
You can find every single entry of your package. json as an environment variable. The accessing is done similar to accessing a property in JSON except that it's all using _ as separators.
Run scripts that set environment across platforms via JSON file. This is a fork of cross-env that uses a JSON file to read the environment variables.
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 .
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.
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