npm-is
Detect the package manager client running the current script. Detects NPM and yarn, as well as any hard executable path.
Last updated 6 months ago by jzetlen .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install npm-is 
SYNC missed versions from official npm registry.

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

> my-yarn-only-monorepo@1.0.0 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();

Current Tags

  • 1.1.0                                ...           latest (6 months ago)

1 Versions

  • 1.1.0                                ...           6 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 3
Dependencies (0)
None
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |