Set and use environment variables in npm scripts across Windows, macOS, and Linux platforms.
cross-env is a Node.js command-line utility that enables developers to set and use environment variables in npm scripts consistently across different operating systems. It solves the problem of platform-specific syntax differences (e.g., `NODE_ENV=production` on POSIX vs `%NODE_ENV%` on Windows) by providing a unified interface. This ensures build scripts, test runners, and other npm tasks work identically on Windows, macOS, and Linux without modification.
Node.js developers and teams who write npm scripts that need to run consistently across multiple operating systems, particularly those collaborating in mixed Windows/POSIX environments.
Developers choose cross-env because it eliminates the need to write and maintain separate script logic for different platforms, reducing complexity and potential errors. Its lightweight, single-purpose design integrates seamlessly into existing npm workflows without overhead.
🔀 Cross platform setting of environment scripts
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Uses POSIX-style syntax (e.g., NODE_ENV=production) that works seamlessly on Windows, macOS, and Linux, eliminating platform-specific script adjustments as described in the problem statement.
Allows setting multiple environment variables in one command, such as FIRST_ENV=one SECOND_ENV=two, simplifying npm script configurations as shown in usage examples.
Supports passing JSON strings with proper escaping for tools like ts-loader, essential for cross-platform execution, though it requires careful syntax with triple backslashes.
Enables handling of signal events like SIGINT on Windows via cross-env-shell, a unique feature that addresses limitations in standard npm scripts on Windows.
Unlike alternatives like env-cmd, cross-env doesn't support reading environment variables from files, forcing all variables to be set inline, which can be cumbersome for many variables.
Setting JSON strings requires triple backslashes and specific syntax, making it error-prone and difficult to maintain, as admitted in the README with warnings about escaping.
Primarily designed for use within npm scripts; using it in general shell contexts or other build systems may not be straightforward or supported, limiting its versatility.