# Right way to set env variable while exec or execFile in nodejs

DevFeed: [Right way to set env variable while exec or execFile in nodejs](<https://devfeed.tech/articles/right-way-to-set-env-variable-while-exec-or-execfile-in-nodejs-21526.md>)

Original publisher: [Read original article](<http://lifepluslinux.blogspot.com/2016/09/right-way-to-set-env-variable-while.html>)

Author: Suresh Alse (noreply@blogger.com)

Published: 2016-09-15T22:02:00Z

Content type: tutorial

Language: en

Sources: [Life Plus Linux](<https://devfeed.tech/sources/life-plus-linux.md>)

Topics: [Environment Variables](<https://devfeed.tech/topics/environment-variables.md>), [Processes](<https://devfeed.tech/topics/processes.md>)

Tags: [environment-variables](<https://devfeed.tech/tags/environment-variables.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [nodejs](<https://devfeed.tech/tags/nodejs.md>), [process](<https://devfeed.tech/tags/process.md>), [variable](<https://devfeed.tech/tags/variable.md>)

## AI overview

This tutorial explains that Node.js exec and execFile replace the existing environment when an env option is supplied, rather than adding variables to it. It recommends copying process.env and modifying the copy.

## Source excerpt

According to the official documentation, exec allows you to pass additional environment variables as part of options like this: This looks fine right? except that it totally isn't! By passing "env" as an option, you are not adding on to existing environment variables, but you are replacing it. This is not clear from the documentation and can leave you scratching your head for a while as it can seem to break the command for no particular reason at all! So, you need to essentially make a copy of process.env and modify it like follows. Thanks for stopping by!