@darkobits/is-dev

Check if your package is being installed locally or by another package.

Stats

StarsIssuesVersionUpdatedCreatedSize
@darkobits/is-dev
000.6.34 months ago4 years agoMinified + gzip package size for @darkobits/is-dev in KB

Readme

This package provides a way to determine if your package is being installed locally (ex: npm install) or by another package (ex: npm install <your package>).

Installation

$ npm install @darkobits/is-dev

Usage

This package is designed to be used as part of a lifecycle script or in files that run during lifecycle scripts. It provides a function, isDev, and two binaries, if-dev and if-not-dev.

isDev(): boolean

Returns true if the package is the host package, false if it is a dependency or if the package is installed globally.

postinstall.js

import isDev from '@darkobits/is-dev';

if (isDev()) {
  // This package is the host package.
} else {
  // This package is a dependency.
}

Binaries

if-dev checks if the package is the host package. If it is, the provided command will be executed.

package.json

{
  "scripts": {
    "bar": "cowsay 'Moo!'",
    "foo": "if-dev npm run bar"
  }
}

if-not-dev is the inverse of if-dev.

Use Cases

NPM runs the following lifecycle scripts (among others) in the following order:

Script Runs Typical Use Case
install Always Set up package for use by consuming packages.
postinstall Always See above.
prepare Development (npm install) Only Generate package's build artifacts.

Because modern JavaScript projects have begun to rely heavily on transpilation, this order of execution is less than ideal. If, for example, we are developing a package that uses a postinstall script that must be transpiled, it will not have been built when the install and postinstall lifecycle scripts are run.

Take the following package.json snippet, for example:

{
  "files": [
    "dist"
  ],
  "scripts": {
    "build": "babel src --out-dir dist",
    "prepare": "npm run build",
    "postinstall": "./dist/bin/postinstall.js"
  }
}

This will work fine for consumers of our package, who will only be downloading our build artifacts (re: dist). But when we try to run npm install when this package is the host, we will get an error, because the package has not been prepared yet, and our dist folder does not exist.

We can use if-dev to address this problem:

package.json

{
  "scripts": {
    "build": "babel src --out-dir dist",
    "prepare": "npm run build",
    "postinstall": "if-dev npm run prepare && ./dist/postinstall.js"
  }
}

This will ensure that for local development, the package is prepared before we try to execute postinstall.js. When the package is being installed by consumers, if-dev will simply no-op and postinstall.js will be run immediately.


Alternatively, imagine we have a postinstall script that we don't want to run during local development. We can use if-not-dev to no-op the postinstall script/command unless the package is being installed by a consumer:

{
  "scripts": {
    "build": "babel src --out-dir dist",
    "prepare": "npm run build",
    "postinstall": "if-not-dev ./dist/postinstall.js"
  }
}

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.