node.js配置npm-NODE_CONFIG_ENV [英] node.js config npm - NODE_CONFIG_ENV
问题描述
我正在尝试使用config软件包(npm install config)来针对不同的环境使用不同的配置。
我正在Windows 10上运行它。
I am trying to use the config package (npm install config) to be able to use different configurations for different enviromenets. I am running it on windows 10.
我在config文件夹下有4个文件:default.json,development.json,production.json和qa。
I got 4 files under the config folder : default.json, development.json, production.json and qa.json.
例如,当我运行 SET NODE_ENV = production
时,它适用于
,但 config
仍然不会从正确的文件中获取信息。
when i am running SET NODE_ENV=production
for example it applys to it
but config
still aint taking the info from the right file.
var config = require('config');
var port = config.get('appPort');
我做了一些阅读,发现了另一个值- NODE_CONFIG_ENV
。
我做了一些测试:
I done some reading and i found out about another value - NODE_CONFIG_ENV
.
I done some testing with :
console.log('NODE_CONFIG_ENV: ' + config.util.getEnv('NODE_CONFIG_ENV'));
console.log('NODE_ENV: ' + config.util.getEnv('NODE_ENV'));
似乎 NODE_CONFIG_ENV
负责这个问题,因为似乎 config
正在使用它来决定选择哪个文件。
And it seems that NODE_CONFIG_ENV
is responsible for the problem because it seems that config
is using it instead to decide which file to choose.
我的问题是我可以使 config
再次使用 NODE_ENV
吗?
或者如果不可能的话设置 NODE_CONFIG_ENV
代替?
My question is how can i make config
use NODE_ENV
again?
Or if it is not possible how can i set NODE_CONFIG_ENV
instead?
推荐答案
这是部分解决方案,
"scripts": {
"dev": "SET NODE_CONFIG_ENV=development&&SET NODE_ENV=development&& nodemon server.js",
"qa": "SET NODE__CONFIG_ENV=qa&&SET NODE_ENV=qa&& node server.js",
"prod": "SET NODE_CONFIG_ENV=production&&SET NODE_ENV=production&& node server.js",
}
NODE_ENV
以防万一它恢复使用,但总的来说,我仍然不知道是什么原因导致它使用了 NODE_CONF IG_ENV
而不是 NODE_ENV
。
I added NODE_ENV
in case it returns back to using it but overall I still didnt figure out what caused it to use NODE_CONFIG_ENV
instead of NODE_ENV
.
编辑:我找到了原因!这是因为我之前使用了另一个名为 cross-env
的npm软件包。
I found the reason! It was because of another npm package called cross-env
which i used earlier.
这篇关于node.js配置npm-NODE_CONFIG_ENV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!