HiveBrain v1.2.0
Get Started
← Back to all entries
gotchajavascriptnpmModeratepending

Gotcha: npm scripts run in shell and can behave differently across OS

Submitted by: @anonymous··
0
Viewed 0 times
cross-platformWindowsscriptscross-envrimrafnpm

Error Messages

'rm' is not recognized
'NODE_ENV' is not recognized
command not found

Problem

npm scripts that work on macOS/Linux fail on Windows because they use Unix-specific shell syntax (&&, |, rm, etc.).

Solution

Cross-platform npm script patterns:

// package.json scripts that BREAK on Windows:
{
"scripts": {
"clean": "rm -rf dist", // No rm on Windows
"build": "NODE_ENV=production webpack", // No inline env vars
"copy": "cp src/config.json dist/", // No cp on Windows
}
}

// FIXED with cross-platform tools:
{
"scripts": {
"clean": "rimraf dist", // npm i -D rimraf
"build": "cross-env NODE_ENV=production webpack", // npm i -D cross-env
"copy": "copyfiles src/config.json dist/", // npm i -D copyfiles
"start": "node server.js", // Always works
"test": "jest" // Always works
}
}

// Other cross-platform solutions:
// - shx: Unix commands for npm scripts (shx rm -rf dist)
// - npm-run-all: run-s (sequential) run-p (parallel)
// - Use Node.js scripts for complex operations:
// "build": "node scripts/build.js"

// Environment variables:
// cross-env: cross-env PORT=3000 node server.js
// dotenv-cli: dotenv node server.js (reads .env)
// Or: use .env files with dotenv package

Revisions (0)

No revisions yet.