javascript node.js process.env:将 process.env 属性分配给字符串类型的未定义结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10265208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
node.js process.env: assigning process.env property to undefined results in string type?
提问by Leftium
The node.js process.env
object seems to process property assignment differently than regular JavaScript objects. How can I get the process.env
object to act like a regular object in this case?
node.jsprocess.env
对象似乎以不同于常规 JavaScript 对象的方式处理属性分配。process.env
在这种情况下,如何让对象像普通对象一样工作?
Below is sample code illustrating the different assignment behavior. For some reason assigning undefined
to a property results in a string type (only for process.env
):
下面是说明不同分配行为的示例代码。出于某种原因,分配undefined
给属性会导致字符串类型(仅适用于process.env
):
function demo(description, dict) {
console.log(description);
dict.A = undefined;
console.log('typeof dict.A: ' + typeof dict.A + '\n');
}
demo('Passing empty object:', {});
demo('Passing process.env:', process.env);
The resulting output is different depending on if an empty object {}
or the process.env
object was passed:
根据传递的是空对象{}
还是process.env
对象,结果输出会有所不同:
$ node test.js Passing empty object: typeof dict.A: undefined Passing process.env: typeof dict.A: string
回答by LukeGT
The process.env
object forces all of its properties to be of type string, since environment variables must always be strings. I'm not entirely sure on your purpose, but maybe you could try one of these as a workaround:
该process.env
对象强制其所有属性都是字符串类型,因为环境变量必须始终是字符串。我不完全确定您的目的,但也许您可以尝试以下方法之一:
Copy the
process.env
object into a new object, which will then behave normally:envCopy = {}; for (e in process.env) envCopy[e] = process.env[e];
Assign
''
to a property instead if you wish it to be 'blank'process.env.A = '';
Which will then return false when you treat it as a boolean
if (process.env.A) { ... }
Or as Jonathan Lonowskipoints out, you can also
delete
the key fromprocess.env
delete process.env.A;
将
process.env
对象复制到一个新对象中,然后该对象将正常运行:envCopy = {}; for (e in process.env) envCopy[e] = process.env[e];
''
如果您希望它为“空白”,请改为分配给属性process.env.A = '';
当您将其视为布尔值时,它将返回 false
if (process.env.A) { ... }
或者正如Jonathan Lonowski指出的那样,您也可以
delete
从process.env
delete process.env.A;
Hope this helps
希望这可以帮助
回答by Jonathan Lonowski
This is occurring because process.env
forces all of its values to String
:
发生这种情况是因为process.env
强制其所有值String
:
process.env.A = undefined;
console.log(process.env.A); // 'undefined' (note the quotes)
process.env.A = true;
console.log(process.env.A); // 'true'
console.log(typeof process.env.A); // 'string'
If you need to remove an environment variable, you'll have to delete
it:
如果您需要删除环境变量,则必须这样delete
做:
function demo(description, dict) {
console.log(description);
delete dict.A;
console.log('typeof dict.A: ' + typeof dict.A + '\n');
}
demo('Passing process.env:', process.env);
// Passing process.env:
// typeof dict.A: undefined