Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
161
rated 0 times [  165] [ 4]  / answers: 1 / hits: 43308  / 7 Years ago, mon, march 6, 2017, 12:00:00

I am trying to set and retrieve node app process.env vars using package.json, so by researching the issue, I've found an example to set / retrieve process.env through the 'config' section, so I added a new config section as shown below :



config : { var1 : test, var2 : test2, var3 : test3 },


But I couldn't access any of the above vars from server.js using for example:



console.log(process.env.npm_package_config_var1); 


So I was wondering how I can set / retrieve process.env var using package.json? Thanks



*I am using npm 4.4.1, node 7.4.0 and I run the app using (npm run dev)


More From » node.js

 Answers
2

You cannot just set environment variables in package.json.



You can set them in your script sections using:



scripts: {
start: ENV_VAR=abc node app.js,
},


or:



 scripts: {
start: cross-env ENV_VAR=abc node app.js,
},


using the cross-env module. See:





Environment variables are something that your programs get at runtime, not something stored in a config - unless you use something like dotenv, see:





but this is using the .env file, not package.json.


[#58664] Friday, March 3, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
raymondd

Total Points: 620
Total Questions: 112
Total Answers: 94

Location: Namibia
Member since Mon, Feb 21, 2022
2 Years ago
raymondd questions
Thu, Apr 22, 21, 00:00, 3 Years ago
Thu, Jul 9, 20, 00:00, 4 Years ago
Thu, Apr 9, 20, 00:00, 4 Years ago
Thu, Jul 25, 19, 00:00, 5 Years ago
;