admin管理员组文章数量:1180440
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)
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)
Share Improve this question asked Mar 6, 2017 at 11:09 MChanMChan 7,16227 gold badges85 silver badges134 bronze badges4 Answers
Reset to default 20You 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:
- https://www.npmjs.com/package/cross-env
Environment variables are something that your programs get at runtime, not something stored in a config - unless you use something like dotenv, see:
- https://www.npmjs.com/package/dotenv
but this is using the .env
file, not package.json.
You cannot just set environment variables in package.json.
Yes you can.
You may try out node -p process.env
as your npm script to inspect your env variable. And ensure that nothing else overwrites your values. Here is another example which works for me.
I don't really understand, what are you trying to do.
But if you want to retrieve env variables you have to do define your dev script in your package.json
like this : NODE_ENV=dev node index.js
Then fetch your env with : process.env.NODE_ENV
This will only work if you start your application using npm:
so have:
"scripts": {
"start": "node server.js"
},
"config" : { "var1" : "test", "var2" : "test2", "var3" : "test3" }
then:
npm run start
and within server.js this will display the variable correctly:
console.log(process.env.npm_package_config_var1);
本文标签: javascriptSetting processenv var in packagejsonStack Overflow
版权声明:本文标题:javascript - Setting process.env var in package.json - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738145089a2065888.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论