🛠️
Utility8M+/wkMIT

cross-env

cross-env is a utility that allows you to set environment variables inline in npm scripts in a way that works across Windows, macOS, and Linux. On Unix-like sys

Installation

npm
npm install -D cross-env
yarn
yarn add -D cross-env
pnpm
pnpm add -D cross-env

Import

ESM
// Used in package.json scripts

Quick Example

usage
// package.json
{
  "scripts": {
    "build": "cross-env NODE_ENV=production webpack",
    "test": "cross-env NODE_ENV=test jest",
    "dev": "cross-env PORT=4000 DEBUG=app:* node server.js"
  }
}

About cross-env

cross-env is a utility that allows you to set environment variables inline in npm scripts in a way that works across Windows, macOS, and Linux. On Unix-like systems, setting environment variables inline is straightforward (NODE_ENV=production node app.js), but Windows Command Prompt and PowerShell use different syntax (set NODE_ENV=production), creating cross-platform compatibility issues in package.json scripts. cross-env normalizes this by providing a command wrapper: cross-env NODE_ENV=production node app.js works identically on all platforms. The tool parses the command line, sets the specified environment variables using Node.js's child_process.spawn, and executes the remaining command in a child process. cross-env supports multiple environment variables, quoted values containing spaces, and variable expansion. The cross-env-shell variant uses shell execution for commands that need shell features like piping and globbing. While newer approaches like dotenv or Node.js's --env-file flag reduce the need for inline environment variables, cross-env remains essential for npm scripts that need to work across development environments with different operating systems. It is one of the most widely used cross-platform utilities in the npm ecosystem.

Quick Facts

Packagecross-env
CategoryUtility
Weekly Downloads8M+
LicenseMIT
Installnpm install -D cross-env

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.