简体   繁体   English

node.js process.env:将 process.env 属性分配给未定义的字符串类型结果?

[英]node.js process.env: assigning process.env property to undefined results in string type?

The node.js process.env object seems to process property assignment differently than regular JavaScript objects. node.js process.env object 处理属性分配的方式似乎与常规 JavaScript 对象不同。 How can I get the process.env object to act like a regular object in this case?在这种情况下,如何让process.env object 像普通的 object 一样工作?

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:生成的 output 会有所不同,具体取决于是否传递了空的 object {}process.env object:

$ node test.js
Passing empty object:
typeof dict.A: undefined

Passing process.env:
typeof dict.A: string

The process.env object forces all of its properties to be of type string, since environment variables must always be strings. process.env对象强制其所有属性都是string类型,因为环境变量必须始终是字符串。 I'm not entirely sure on your purpose, but maybe you could try one of these as a workaround: 我不完全确定你的目的,但也许你可以尝试其中一种作为解决方法:

  • Copy the process.env object into a new object, which will then behave normally: process.env对象复制到一个新对象中,然后该对象将正常运行:

     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 当您将其视为布尔值时,将返回false

     if (process.env.A) { ... } 
  • Or as Jonathan Lonowski points out, you can also delete the key from process.env 或者正如Jonathan Lonowski指出的那样,您也可以从process.env delete密钥

     delete process.env.A; 

Hope this helps 希望这可以帮助

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

You can bypass process.env 's string coercion by using Object.defineProperty instead:您可以使用Object.defineProperty绕过process.env的字符串强制:

Object.defineProperty(process.env, 'ENV_VAR_NAME', {
  configurable: true,
  value: nonStringValue
});

configurable: true is required if you want to do delete process.env.ENV_VAR_NAME later on. configurable: true如果您想稍后delete process.env.ENV_VAR_NAME ,则需要 true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM