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?
To unset an environment variable from Command Prompt, type the command setx variable_name “”. For example, we typed setx TEST “” and this environment variable now had an empty value.
The dotenv package for handling environment variables is the most popular option in the Node. js community. You can create an. env file in the application's root directory that contains key/value pairs defining the project's required environment variables.
NODE_ENV is an environment variable that stands for node environment in express server. The NODE_ENV environment variable specifies the environment in which an application is running (usually, development or production).
There is another unintuitive aspect to it: Node.js converts undefined
to the string "undefined" when assigning an environment variable:
> process.env.MYVAR = undefined
undefined
> typeof process.env.MYVAR
'string'
You can work around this using delete
:
> delete process.env.MYVAR
true
> typeof process.env.MYVAR
'undefined'
Tested with Node.js 13.5, 12.14 and 10.18.
For this reason (process.env.MYVAR || '')
does not help, as it evaluates to ('undefined' || '')
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With