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 install -D cross-env
yarn add -D cross-env
pnpm add -D cross-env
Import
// Used in package.json scripts
Quick Example
// 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
| Package | cross-env |
| Category | Utility |
| Weekly Downloads | 8M+ |
| License | MIT |
| Install | npm install -D cross-env |
Related Packages
dotenv is a zero-dependency module that loads environment variables from a .env file into process.en…
Concurrently is a utility for running multiple commands simultaneously in a single terminal, with ea…
Nodemon is a utility that monitors for file changes in your Node.js application and automatically re…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.