|
1 | 1 | ---
|
2 | 2 | id: only-allow-pnpm
|
3 |
| -title: Only allow pnpm |
| 3 | +title: Only Allow PNPM |
4 | 4 | ---
|
5 | 5 |
|
6 |
| -When you use pnpm on a project, you don't want others to accidentally run |
7 |
| -`npm install` or `yarn`. To prevent devs from using other package managers, |
8 |
| -you can add the following `preinstall` script to your `package.json`: |
| 6 | +When many developers are working on the same project together, you need a failsafe in case someone accidentally runs commands with another package manager (like NPM, Yarn, Bun). |
9 | 7 |
|
10 |
| -```json |
| 8 | +To prevent dependency management conflicts between package managers: |
| 9 | + |
| 10 | +1. Create a file, if it doesn't already exist, named `.npmrc` at the root of your project. |
| 11 | +2. Write the following content into your `.npmrc`: |
| 12 | + |
| 13 | +``` |
| 14 | +engine-strict=true |
| 15 | +``` |
| 16 | + |
| 17 | +3. Write the following content into your `package.json`: |
| 18 | + |
| 19 | +``` |
11 | 20 | {
|
12 |
| - "scripts": { |
13 |
| - "preinstall": "npx only-allow pnpm" |
14 |
| - } |
| 21 | + "devEngines": { |
| 22 | + "runtime": { |
| 23 | + "name": "node", |
| 24 | + "onFail": "error" |
| 25 | + }, |
| 26 | + "packageManager": { |
| 27 | + "name": "pnpm", |
| 28 | + "onFail": "error" |
| 29 | + } |
| 30 | + }, |
| 31 | + "engines": { |
| 32 | + "node": ">=18.18.0", |
| 33 | + "pnpm": ">=10.0.0" |
| 34 | + }, |
15 | 35 | }
|
16 | 36 | ```
|
17 | 37 |
|
18 |
| -Now, whenever someone runs `npm install` or `yarn`, they'll get an |
19 |
| -error instead and installation will not proceed. |
| 38 | +- Now, when you run `npm i`, `npm i -D` (or an equivalent), these commands return this error (before the preinstall script can run): |
20 | 39 |
|
21 |
| -If you use npm v7, use `npx -y` instead. |
| 40 | +``` |
| 41 | +username@hostname some-project % npm i -D package |
| 42 | +npm error code EBADDEVENGINES |
| 43 | +npm error EBADDEVENGINES The developer of this package has specified the following through devEngines |
| 44 | +npm error EBADDEVENGINES Invalid engine "packageManager" |
| 45 | +npm error EBADDEVENGINES Invalid name "pnpm" does not match "npm" for "packageManager" |
| 46 | +npm error EBADDEVENGINES { |
| 47 | +npm error EBADDEVENGINES current: { name: 'npm', version: '10.0.0' }, |
| 48 | +npm error EBADDEVENGINES required: { name: 'pnpm', onFail: 'error' } |
| 49 | +npm error EBADDEVENGINES } |
| 50 | +npm error A complete log of this run can be found in: /Users/username/.npm/_logs/2021-08-21T00_00_00_000Z-debug-0.log |
| 51 | +``` |
0 commit comments