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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:13:38  来源:igfitidea点击:

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

javascriptnode.js

提问by Leftium

The node.js process.envobject seems to process property assignment differently than regular JavaScript objects. How can I get the process.envobject 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 undefinedto 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.envobject 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.envobject 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.envobject 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 deletethe key from process.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指出的那样,您也可以deleteprocess.env

    delete process.env.A;
    

Hope this helps

希望这可以帮助

回答by Jonathan Lonowski

This is occurring because process.envforces 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 deleteit:

如果您需要删除环境变量,则必须这样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