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

npm-is

v1.1.0

Published

Detect the package manager client running the current script. Detects NPM and yarn, as well as any hard executable path.

Downloads

8

Readme

Detect the package manager client running the current script. Detects NPM and yarn, as well as any hard executable path.

Use this when your project requires a specific package manager to work correctly, such as a monorepo using Yarn workspaces. It can help your package scripts exit early if they're being invoked outside of the required package manager.

Recommended Usage

In a monorepo, or any repo with private: true, it's safe to catch things as early as possible. Add a preinstall script to prevent the wrong package manager from even beginning to install dependencies. Use npx to dynamically install npm-is before any other modules are installed.

 {
   "name": "my-yarn-only-monorepo",
   "version": "1.0.0",
   "private": true,
   "workspaces": [
     "packages/*"
   ]
   "scripts": {
+    "preinstall": "npx npm-is yarn"
   }
 }

Now, this happens when you try to npm install.

$ npm install

> [email protected] preinstall /Users/me/repo
> npx npm-is yarn

/Users/me/repo/node_modules/npm-is/npm-is.js:54
    throw new WrongPackageManagerError(allowed, invoked);
    ^

Error: This project can only be used with the "yarn" package manager, but it was
invoked by "npm", which is not supported.

Install

Don't install this as a dependency if you're going to use it in the recommended way described above.

Instead, run it with npx so that it works before dependencies are installed!

But if you insist:

with npm: npm install --save-dev npm-is

with yarn: yarn add --dev npm-is

Advanced Usage

CLI

With no arguments, npm-is prints the current package manager. So, running it outside a package manager with no arguments, it will print nothing. Running it inside an NPM script with NPM should print npm to the command line.

With arguments, npm-is will take each of its arguments to be allowed package managers. If the preinstall script is "npm-is yarn npm", then install will only succeed if the package manager in use is Yarn or NPM. Only npm and yarn are currently supported as shorthand. If you're using a custom package manager, you must know its exec path (that is, the value of the environment variable $npm_execpath when it is running lifecycle scripts) and pass that as an argument. If the preinstall script is "npm-is /path/to/pnpm, then install will only succeed if $npm_execpath is exactly /path/to/pnpm.

Node API

const npmIs = require("npm-is");

// returns true if Yarn
npmIs("yarn");
// returns true if Yarn or NPM
npmIs(["yarn", "npm"]);
// returns true if Yarn, NPM, or custom executable
npmIs(["yarn", "npm", "/some/custom/one"]);

// Throws instead of returning false
npmIs.assert("yarn");
npmIs.assert(["yarn", "npm"]);
npmIs.assert(["yarn", "npm", "/some/custom/one"]);

// Returns "npm" if NPM, "yarn" if Yarn,
// empty string if no package manager,
// or full execPath if anything else
npmIs.detect();