Windows Alternative to “source .env” for Setting Environment Variables

Matthew Bennett
3 min readDec 7, 2020

--

a nest sitting on a porch
Photo by Kelly Sikkema on Unsplash

I recently started working on a NestJS/TypeORM project that has several environment variables, including values used in connecting to a database, third-party API keys, and more. The existing method for setting environment variables was to call source .env before any command in package.json that needed the environment variables to be set.

For example:

"scripts": {
...
"start": "source .env && nest start",
...
},

However, if you try running this on a Windows machine, your shell will most likely respond with:

‘source’ is not recognized as an internal or external command, operable program or batch file.

I received this same response when running this command in Windows PowerShell, Command Prompt, and Git Bash.

source is a shell command that reads and executes the contents of a file (generally a set of commands) within the current shell context. In the NestJS project mentioned above, .env was the executable file that held several commands, all of which were the same command for setting different environment variables. It looked something like:

export DATABASE_USERNAME=username
export DATABASE_PASSWORD=password
export THIRD_PARTY_API_KEY=thekey

Running source .env would read and execute these commands, setting these values into environment variables, which could then be accessed in our JavaScript with the process.env. prefix. However, as I mentioned above, this does not work on a Windows machine (which is what I was using for local development).

I found that creating and calling a batch file served as a viable Windows alternative to source .env. This involved copying the contents of .env over to a new file, which I named env.bat, and replacing source .env with call env.bat.

Batch files can be used to handle many different tasks, but in this case, I only needed to use the set keyword to set the environment variables.

set DATABASE_USERNAME=username
set DATABASE_PASSWORD=password
set THIRD_PARTY_API_KEY=thekey

For this simple example, we can just replace the export keyword from our .env with set for our batch file (env.bat). The rest of the file can remain as-is. (However, if we had slightly more complicated values, such as ones that contained spaces or special characters, we would possibly need to handle those differently. For example, for values with spaces, we would need to do something like: set "VARIABLE_WITH_SPACES=my env value").

Then, in package.json, I updated the source .env calls to call env.bat.

"scripts": {
...
"start": "call env.bat && nest start",
...
},

This successfully loaded the variables in my shell context so that I could access them in the JavaScript.

Though there are many other ways to set environment variables, I found the batch file method to be the most useful in my case.

Rather than using the source command or the the batch file method described in this post, you may also consider using a third-party npm package, such as dotenv, env-cmd, or, for NestJS specifically, Nest’s own @nestjs/config (which uses dotenv internally).

--

--

No responses yet