|
1 | 1 | # check-npm-client
|
2 | 2 |
|
3 |
| -This module helps to ensure using of [npm](https://github.com/npm/cli) or [yarn](https://github.com/yarnpkg/yarn) as npm client before try to install any dependencies to your project. It is currently working in progress. |
| 3 | +`npm` and `yarn` are both npm clients used to manage node.js project dependencies. They are somehow different in some behavior, and thus can affect project running. |
4 | 4 |
|
5 |
| -## references |
| 5 | +This package provide ready-to-use functionality to check current npm client. When used as [pre](https://docs.npmjs.com/misc/scripts#hook-scripts) commands in npm scripts, it will check if the script is executed by specific npm client (`yarn` or `npm`). If it is restricted, then the script execution will be aborted early. |
| 6 | + |
| 7 | +## Usage Scenario |
| 8 | + |
| 9 | +`yarn` uses [yarn.lock](https://yarnpkg.com/lang/en/docs/yarn-lock/) to ensure versions of dependencies, but `npm` use [package-lock.json](https://docs.npmjs.com/files/package-lock.json) to do that instead. If you use `yarn` to install dependencies in a project which is actually managed by `npm` and `package-lock.json`, the actually installed dependencies in `node_modules` might be different due to [semver](https://docs.npmjs.com/misc/semver) behavior, and this could lead to some un-expected exception while code running. |
| 10 | + |
| 11 | +This is common when working with others because people tends to use `yarn` or `npm` as their wishes. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +$ npm install check-npm-client |
| 17 | +``` |
| 18 | + |
| 19 | +or alternatively, with `yarn`: |
| 20 | + |
| 21 | +```bash |
| 22 | +$ yarn add check-npm-client |
| 23 | +``` |
| 24 | + |
| 25 | +## Example |
| 26 | + |
| 27 | +This package provide a command line tool to invoke checking functionality: |
| 28 | + |
| 29 | +```bash |
| 30 | +# the user must use `npm` to execute the script |
| 31 | +$ check-npm-client --npm-only |
| 32 | + |
| 33 | +# the user must use `yarn` to execute the script |
| 34 | +$ check-npm-client --yarn-only |
| 35 | + |
| 36 | +# automatically check according to current working directory lock files if exists |
| 37 | +# not available yet |
| 38 | +$ check-npm-client |
| 39 | +``` |
| 40 | + |
| 41 | +Besides, you can use it programmatically: |
| 42 | + |
| 43 | +```javascript |
| 44 | +const { checkNpmClient } = require('check-npm-client'); |
| 45 | +console.log(checkNpmClient('yarn')); // true/false |
| 46 | +``` |
| 47 | + |
| 48 | +### Ensure before installation |
| 49 | + |
| 50 | +Add the script in your `package.json` to ensure that user must install dependencies with `yarn`: |
| 51 | + |
| 52 | +```json |
| 53 | +{ |
| 54 | + "scripts": { |
| 55 | + "preinstall": "check-npm-client --yarn-only" |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +**Note**: `yarn` and `npm` treat `preinstall` script a little differently. `npm` execute it only before `npm install` (not before `npm install <module>`), but `yarn` always run it before any installation. See [issue](https://github.com/npm/cli/issues/481) here. So if specified as `--yarn-only` and user use `npm` to install another dependency, the `preinstall` will not be invoked (so the ensurance will fail). |
| 61 | + |
| 62 | +### Ensure before any other script |
| 63 | + |
| 64 | +Add the script in your `package.json` to ensure that user must use `npm` to run the script `my-task`: |
| 65 | + |
| 66 | +```json |
| 67 | +{ |
| 68 | + "scripts": { |
| 69 | + "premy-task": "check-npm-client --npm-only", |
| 70 | + "my-task": "node my-task.js" |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +## References |
6 | 76 |
|
7 |
| -- <https://github.com/nodejs/node/issues/14957> |
8 |
| -- <https://github.com/yibn2008/find-process> |
9 |
| -- <https://github.com/timoxley/npm-which> |
10 | 77 | - <https://github.com/npm/cli/issues/481>
|
| 78 | +- <https://github.com/yibn2008/find-process> |
11 | 79 | - <https://stackoverflow.com/questions/46725374/how-to-run-a-script-before-installing-any-npm-module>
|
12 | 80 | - <https://github.com/yarnpkg/yarn/issues/5063>
|
0 commit comments