简体   繁体   English

如何在Node.js中将环境变量设置为undefined?

[英]How do I set an environment variable to undefined in Node.js?

From the REPL, I have the following code: 从REPL,我有以下代码:

> process.env.MY_VAR=undefined
undefined
> process.env.MY_VAR
'undefined'

How do I set an envirornment variable to be undefined (ie no value), instead of the string 'undefined'? 如何将环境变量设置为未定义(即无值),而不是字符串“ undefined”?

In JavaScript, undefined means that the thing can be accessed, but has an undefined value. 在JavaScript中, undefined表示可以访问事物,但具有未定义的值。 You could try and use delete , like: 您可以尝试使用delete ,例如:

x = {a:1, b:2, c:3};
delete x.b;
// x is now {a:1, c:3}

But I am not sure you can do that with env properties, and even if you can, be aware that this change will be applied only to YOUR scope. 但是我不确定您是否可以使用env属性来做到这一点,即使您可以,也请注意,此更改将仅应用于您的范围。 I mean, other scripts relying on that same environment variable will still have it. 我的意思是,依赖于相同环境变量的其他脚本仍将拥有它。 That's because when you run a script in node, it creates its own environment to execute, copying the current env into its own scope. 这是因为当您在节点中运行脚本时,它会创建自己的环境来执行,将当前环境复制到自己的作用域中。

If you haven't assigned the variable to anything, it should return undefined . 如果尚未将变量分配给任何变量,则应返回undefined If you assign it undefined , implicit type coercion steps in and changes it to a string. 如果将其分配为undefined ,则隐式类型强制将进入并将其更改为字符串。

If you're trying to unset an existing variable, you could use delete process.env.MY_VAR and then it will return undefined . 如果尝试取消设置现有变量,则可以使用delete process.env.MY_VAR ,然后它将返回undefined

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

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