npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@darkobits/is-dev

v0.6.3

Published

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

Downloads

26

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"
  }
}