[英]NodeJS - config cannot not load custom environment variables
I am running config@1.30.0
and I am attempting to get settings from the environment variables using .\\config\\custom-environment-variables.json
does not work.我正在运行config@1.30.0
并且我试图使用.\\config\\custom-environment-variables.json
从环境变量中获取设置不起作用。 However, it reads from the .\\config\\default.json
just fine.但是,它从.\\config\\default.json
读取就好了。
.\\config\\custom-environment-variables.json
{
"key": "app_key"
}
.\\config\\default.json
{
"key": "defaultKey"
}
running跑步
const config = require('config');
console.log(config.get('key'))
always prints总是打印
defaultKey
but prints nothing when I set the key
property in config/default to an empty string.但是当我将 config/default 中的key
属性设置为空字符串时,什么都不打印。 How can I resolve this?我该如何解决这个问题?
The config file name relates to the NODE_ENV environment variable you use when starting node.配置文件名与您在启动 node.js 时使用的 NODE_ENV 环境变量相关。
The purpose of the module is to have a config file for each type of environment you are deploying to test, staging, prod environments.该模块的目的是为您部署到测试、暂存、生产环境的每种类型的环境提供一个配置文件。 Default takes over if nothing is set or a file can't be find如果未设置任何内容或找不到文件,则默认接管
eg for test and staging environments you would have.例如,对于您将拥有的测试和登台环境。
config/default.json
{
"key": "default_key"
}
config/test.json
{
"key": "test_key"
}
config/production.json
{
"key": "prod_key"
}
app.js
var config = require('config')
console.log(config.key)
Then if you run with a different NODE_ENV the same name as the file in the config directory you get the different keys然后,如果您使用与 config 目录中的文件名称相同的不同 NODE_ENV 运行,您将获得不同的键
node app.js // output default_key
NODE_ENV=test node app.js // output test_key
NODE_ENV=production node app.js // output prod_key
You question references custom environment variables using the file config/custom-environment-variables.json
This file will enable you to override a value in one of the files with a environment variable set when running node.您使用文件config/custom-environment-variables.json
质疑引用自定义环境变量 此文件将使您能够在运行节点时使用环境变量集覆盖其中一个文件中的值。 This is useful when you can't commit the variable, such as database key but might want to access all your config in the same place.当您无法提交变量(例如数据库键)但可能希望在同一位置访问所有配置时,这很有用。
eg例如
{
"key": "SECURE_DATABASE_KEY"
}
Then running the same program again with the new config file:然后使用新的配置文件再次运行相同的程序:
NODE_ENV=production node app.js // output prod_key
SECURE_DATABASE_KEY=asldfj40 NODE_ENV=production node app.js // output asldfj40
I ran into a similar problem and found that if I don't open a new terminal window and I restart the server in the same window where I run export some_secret=immasecret
, then the app doesn't crash and the some_secret
variable can be accessed.我遇到了类似的问题,发现如果我不打开新的终端窗口并在运行export some_secret=immasecret
的同一窗口中重新启动服务器,则应用程序不会崩溃并且可以访问some_secret
变量. I'd previously been trying to access the variable while running node in another window.我以前一直试图在另一个窗口中运行节点时访问该变量。
A solution is custom-env nodejs module, it allows you to add different environment variables for different stages using the popular .env
method.一个解决方案是custom-env nodejs 模块,它允许您使用流行的.env
方法为不同阶段添加不同的环境变量。 Example .env
for dev
environment and .env.staging
for staging
environment示例.env
用于dev
环境和.env.staging
用于staging
环境
This issue is with VSCODE Editors Integrated Terminal此问题与 VSCODE 编辑器集成终端有关
We have also struggled a lot with this issue initially, but the issue is that you might be using the **integrated terminal** that comes with *VSCODE* there is an issue with that, please try to use some external terminals like *cmder* or cmd prompt that comes with windows you will get the output as you are expecting.
USE EXTERNAL TERMINAL OR CMD PROMPT to execute the code使用 EXTERNAL TERMINAL 或 CMD PROMPT 执行代码
Your files and codes are correct your cmd command is wrong你的文件和代码是正确的 你的 cmd 命令是错误的
use this command使用这个命令
setx app_key NewKey setx app_key NewKey
Attention!注意力!
If config/production.json如果 config/production.json
{
"key": "prod_key"
}
and config/local.json和 config/local.json
{
"key": "local_key"
}
and和
NODE_ENV=production node app.js
the Output is: local_key输出是:local_key
If a local.json exist is NODE_ENV=production is ignored如果 local.json 存在则 NODE_ENV=production 被忽略
Details s.详情config Wiki (it is very refined, unfortunately too few examples) config Wiki (很精致,可惜例子太少)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.