The node.js process.env object seems to process property assignment differently than regular JavaScript objects. How can I get the process.env object to act like a regular object in this case?
Below is sample code illustrating the different assignment behavior. For some reason assigning undefined to a property results in a string type (only for 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.env object was passed:
$ node test.js Passing empty object: typeof dict.A: undefined Passing process.env: typeof dict.A: string
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:Copy the
process.envobject into a new object, which will then behave normally:Assign
''to a property instead if you wish it to be ‘blank’Which will then return false when you treat it as a boolean
Or as Jonathan Lonowski points out, you can also
deletethe key fromprocess.envHope this helps