Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including cross-env with all npm packages installed. Try it out:

var crossEnv = require("cross-env")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

cross-env v4.0.0

Run scripts that set and use environment variables across platforms


Run scripts that set and use environment variables across platforms

Travis Build Status AppVeyor Build Status Code Coverage Dependencies version node-version downloads

MIT License All Contributors PRs Welcome Donate Code of Conduct Roadmap Examples

Watch on GitHub Star on GitHub Tweet

The problem

Most Windows command prompts will choke when you set environment variables with NODE_ENV=production like that. (The exception is Bash on Windows, which uses native Bash.) Similarly, there's a difference in how windows and POSIX commands utilize environment variables. With POSIX, you use: $ENV_VAR and on windows you use %ENV_VAR%.

This solution

cross-env makes it so you can have a single command without worrying about setting or using the environment variable properly for the platform. Just set it like you would if it's running on a POSIX system, and cross-env will take care of setting it properly.


This module is distributed via npm which is bundled with node and should be installed as one of your project's devDependencies:

npm install --save-dev cross-env


I use this in my npm scripts:

  "scripts": {
    "build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"

Ultimately, the command that is executed (using cross-spawn) is:

webpack --config build/webpack.config.js

The NODE_ENV environment variable will be set by cross-env

You can also split a command into several ones, or separate the environment variables declaration from the actual command execution. You can do it this way:

  "scripts": {
    "parentScript": "cross-env GREET=\"Joe\" npm run childScript",
    "childScript": "echo Hello $GREET"

Where childScript holds the actual command to execute and parentScript sets the environment variables to use. Then instead of run the childScript you run the parent. This is quite useful for launching the same command with different env variables or when the environment variables are too long to have everything in one line.


If you want to have the environment variable apply to several commands in series then you will need to wrap those in quotes in your script. For example:

  "scripts": {
    "greet": "cross-env GREETING=Hi NAME=Joe \"echo $GREETING && echo $NAME\""


I originally created this to solve a problem I was having with my npm scripts in angular-formly. This made it made contributing to the project much easier for windows users.

Other Solutions

  • env-cmd - Reads environment variables from a file instead


Thanks goes to these people (emoji key):

| [
Kent C. Dodds](
[💻]( [📖]( 🚇 [⚠️]( | [
Ya Zhuang ](
🔌 [📖]( | [
James Harris](
[📖]( | [
[🐛]( [📖]( [⚠️]( | [
Daniel Rodríguez Rivero](
[🐛]( [💻]( [📖]( | [
Jonas Keinholz](
[🐛]( [💻]( [⚠️]( | [
Hugo Wood](
[🐛]( [💻]( [⚠️]( | | :---: | :---: | :---: | :---: | :---: | :---: | :---: | | [
Thiebaud Thomas](
[🐛]( [💻]( [⚠️]( | [
Daniel Rey López](
[💻]( [⚠️]( |

This project follows the all-contributors specification. Contributions of any kind welcome!

Note: this was added late into the project. If you've contributed to this project in any way, please make a pull request to add yourself to the list by following the instructions in the



RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free