Skip to content

Instantly share code, notes, and snippets.

@haggen
Created May 17, 2022 22:42
Show Gist options
  • Save haggen/5b925f08221b0aa022346b8e86712bfb to your computer and use it in GitHub Desktop.
Save haggen/5b925f08221b0aa022346b8e86712bfb to your computer and use it in GitHub Desktop.
Run npm scripts per NODE_ENV value.

In your package.json:

"scripts": {
  "start": "per-env",
  "start:development": "echo We're in development!",
  "start:production": "echo We're in production!",
}

Then call npm start. It'll read from NODE_ENV (defaulting to development) and run the appropriate script.

Inspired by https://github.com/ericclemmons/per-env.

#!/usr/bin/env bash
set -euo pipefail
if test -z "${npm_lifecycle_event:-}"; then
echo "per-env: not called from a npm lifecycle event"
exit 1
fi
npm run-script "${npm_lifecycle_event}:${NODE_ENV:=development}" -- "$@"
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment