Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js unset environment variable

Tags:

node.js

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?

like image 990
danday74 Avatar asked Apr 10 '17 02:04

danday74


People also ask

How do I unset an environment variable?

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.

What is .env file in Node js?

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.

What is Node_env in Node js?

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).


1 Answers

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' || '').

like image 165
Simon Warta Avatar answered Oct 08 '22 11:10

Simon Warta