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?
的节点。js的过程。env对象处理属性分配的方式似乎与普通的JavaScript对象不同。我怎样才能得到这个过程。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
):
下面是说明不同赋值行为的示例代码。由于某些原因,将未定义的属性分配给属性会导致字符串类型(仅针对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:
结果输出根据空对象{}或进程不同而不同。env对象通过:
$ node test.js Passing empty object: typeof dict.A: undefined Passing process.env: typeof dict.A: string
2 个解决方案
#1
28
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:
这个过程。env对象将其所有属性强制为string类型,因为环境变量必须始终为string。我不完全确定你的目的,但也许你可以试一试其中一种方法:
-
Copy the
process.env
object into a new object, which will then behave normally:复制过程。将env对象转换为一个新对象,该对象将正常运行:
envCopy = {}; for (e in process.env) envCopy[e] = process.env[e];
-
Assign
''
to a property instead if you wish it to be 'blank'如果您希望属性为“blank”,则将其分配给“属性”
process.env.A = '';
Which will then return false when you treat it as a boolean
当你把它当作布尔值时,哪个会返回false
if (process.env.A) { ... }
-
Or as Jonathan Lonowski points out, you can also
delete
the key fromprocess.env
或者像Jonathan Lonowski指出的,你也可以从process.env中删除这个键
delete process.env.A;
Hope this helps
希望这有助于
#2
12
This is occurring because process.env
forces all of its values to String
:
这是因为过程。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:
如果你需要删除一个环境变量,你必须删除它:
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
#1
28
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:
这个过程。env对象将其所有属性强制为string类型,因为环境变量必须始终为string。我不完全确定你的目的,但也许你可以试一试其中一种方法:
-
Copy the
process.env
object into a new object, which will then behave normally:复制过程。将env对象转换为一个新对象,该对象将正常运行:
envCopy = {}; for (e in process.env) envCopy[e] = process.env[e];
-
Assign
''
to a property instead if you wish it to be 'blank'如果您希望属性为“blank”,则将其分配给“属性”
process.env.A = '';
Which will then return false when you treat it as a boolean
当你把它当作布尔值时,哪个会返回false
if (process.env.A) { ... }
-
Or as Jonathan Lonowski points out, you can also
delete
the key fromprocess.env
或者像Jonathan Lonowski指出的,你也可以从process.env中删除这个键
delete process.env.A;
Hope this helps
希望这有助于
#2
12
This is occurring because process.env
forces all of its values to String
:
这是因为过程。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:
如果你需要删除一个环境变量,你必须删除它:
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