Node.js unset environment variable
NickName:danday74 Ask DateTime:2017-04-10T10:36:09

Node.js unset environment variable

How do you unset an environment variable in Node.js?

I've tried:

process.env.MYVAR = undefined

But that doesn't unset it, it appears as follows:

console.log('xx' + process.env.MYVAR + 'xx');

Output is:

xxundefinedxx

I want:

xxxx

How do I make this work?

Copyright Notice:Content Author:「danday74」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43314209/node-js-unset-environment-variable

Answers
ttemple 2022-01-09T12:57:27

Another way to clear a local environment variable is by using unset command (Linux/Mac). To unset any local environment variable temporarily:\n$ unset <var-name>\n\nwhere <var-name> is the name of local variable which you want to unset or clear.",


Simon Warta 2020-02-10T09:10:50

There is another unintuitive aspect to it: Node.js converts undefined to the string "undefined" when assigning an environment variable:\n> process.env.MYVAR = undefined\nundefined\n> typeof process.env.MYVAR\n'string'\n\nYou can work around this using delete:\n> delete process.env.MYVAR\ntrue\n> typeof process.env.MYVAR\n'undefined'\n\nTested with Node.js 10.18, 12.14, 13.5 and 16.15.\nFor this reason (process.env.MYVAR || '') does not help, as it evaluates to ('undefined' || '').",


Dan D. 2017-04-10T02:43:20

The problem is that:\n\nconsole.log('xx' + process.env.MYVAR + 'xx');\n\n\nIs actually incorrect. The value of an undefined property is undefined. process.env.MYVAR = undefined is the same as delete process.env.MYVAR in so far as the value of the property is the same. But properties also have a presence that delete removes, in that the key will not show up in the Array returned by Object.keys.\n\nIf you want the empty string, instead you must write:\n\nconsole.log('xx' + (process.env.MYVAR || '') + 'xx');\n",


meyer9 2017-04-10T02:38:30

Make sure you understand that undefined !== \"\"\n\nUse this: process.env.MYVAR = \"\" if you want an empty string. undefined means there is nothing there. An empty string means there is the text of nothing to make it easy to understand.",


More about “Node.js unset environment variable” related questions

Node.js unset environment variable

How do you unset an environment variable in Node.js? I've tried: process.env.MYVAR = undefined But that doesn't unset it, it appears as follows: console.log('xx' + process.env.MYVAR + '

Show Detail

How to unset an environment variable using php?

I know I can set an environment variable using putenv("ENV_FOO=SOMETHING"); and get the value via: getenv("ENV_FOO"); If the variable isn't set, getenv("ENV_FOO") will return false.&#x

Show Detail

How to unset environment variable in Jenkins

I have a Jenkinsfile and the JAVA_HOME environment variable is already set when the process starts up. Unfortunately, this environment variable is being passed into my container when I run a script...

Show Detail

How to unset an environment variable from docker?

I want to unset the environment variable through entry point scripts. I am sourcing the cleanup.sh in entry point script. But the unsetting of the environment variable is not happening. There ar...

Show Detail

How to log if an environment variable is unset?

How can I write a shell script that checks for an environment variable and writes to a log file if the variable is unset?

Show Detail

Unset an environment variable for a single command

In Bash, we can set an environment variable for a single command this way: FOO=bar somecommand What if we want to unset a variable for a single command?

Show Detail

Unset Environment Variable in grunt-env

Is there a way to unset environment variables with grunt-env? I know I can set them from the module, and there is even a way to set them to a value only if it doesn't exist. However, the project's ...

Show Detail

Unset environment variable when Eclipse application is launched?

I've got a situation where a user has the 'JAVA_TOOL_OPTIONS' environment variable set on their workstation ... but the option is causing my Eclipse application to crash. Is there a way, in the ec...

Show Detail

Unset environment variable in Jupyter Notebook

How do I unset an environmental variable in Jupyter Notebook? I set the variables using a .env file that is loaded by: from dotenv import load_dotenv load_dotenv('./.env') After changing the .env...

Show Detail

Unable to unset the linux environment variable

Unable to unset the linux environment variable FLEXLM_DIAGNOSTICS {goudarsh:tst_rep/}@vilc1199:/&lt;8&gt;ndm&gt; echo $FLEXLM_DIAGNOSTICS 5 {goudarsh:tst_rep/}@vilc1199:/&lt;8&gt;ndm&gt; unset

Show Detail