larryq
larryq

Reputation: 16309

How to set multiple environment variables in a single line on Windows?

I'm working on a Node.js project and using Jest as the test framework. This project runs on Windows as it happens, and I'm having a heck of a time setting more than one environment variable on the command line.

Here's the relevant line in package.json

  "scripts": {
     "test": "SET NODE_ENV=test & SET DB_URI=postgresql://<database stuff>> & jest -t Suite1 --watch --verbose false"
  },

As can be seen above, I'm setting both a NODE_ENV and DB_URI environment variable prior to running jest via npm run test.

My problem is the that DB_URI environment variable doesn't appear to be set when jest runs. The error I get back from jest makes it obvious it can't find it. I do know that the first, NODE_ENV environment variable is set ok, but am not sure what's wrong with the second one, did I get the syntax wrong somehow? Is anyone with jest experience on Windows doing something similar to what I'm trying?

Upvotes: 10

Views: 15695

Answers (3)

Gilbert
Gilbert

Reputation: 3324

If you are setting the environment variables from powershell you can do like so:

cmd /K "set f=df & echo %f%"

The output will be "df"

Upvotes: 0

Teneff
Teneff

Reputation: 32158

I'd suggest you to add cross-env. It should be able to set multiple environment variables for Windows and POSIX

package.json
{
  // ...
  "scripts": {
    "test": "cross-env NODE_ENV=test DB_URI=postgresql://<database stuff>> jest -t Suite1 --watch --verbose false"
  },
  "devDependencies": {
    "cross-env": "^6.0.0"
  }
}

Upvotes: 1

Prabhjot Singh Kainth
Prabhjot Singh Kainth

Reputation: 1861

Just make the following change:

Use &&, also you need to remove the white space before and after the "&&".

"scripts": {
     "test": "SET NODE_ENV=test&&SET DB_URI=postgresql://<database stuff>>&&jest -t Suite1 --watch --verbose false"
  },

Upvotes: 15

Related Questions