Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including if-env with all npm packages installed. Try it out:

if-env lists no main file and has no index.js, so it can't be directly required. If this is a mistake, please let us know. It may however contain internal files that you can require manually:

// require("if-env/[??]")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

if-env v1.0.0

Simplify npm scripts with "if-env ... && npm run this || npm run that"

if-env

Simplify npm scripts with if-env ... && npm run this || npm run that


Suppose you want to simplify development and be able to run npm start in all environments & run the correct scripts.

Your package.json might look like this:

"scripts": {
  "start": "if [[ ${NODE_ENV} == \"production\" ]]; then npm run start:prod; else npm run start:dev; fi",
  "start:dev": "webpack",
  "start:prod": "start-cluster"
}

The problem is, this doesn't work in all environments.

Instead, you can write:

"scripts": {
  "start": "if-env NODE_ENV=production && npm run start:prod || npm start:dev",
  "start:dev": "webpack",
  "start:prod": "start-cluster"
}

Usage

1. Install

$ npm install if-env --save

2. Add to package.json

"scripts": {
  "start": "if-env SOME_ENV_VAR=some_val ANOTHER_ENV_VAR=another_val && npm run this || npm run that"
}

License

MIT © Eric Clemmons 2015

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free