dmalechek
dmalechek

Reputation: 150

trouble with npm preinstall script

I'm trying to make the jump to pnpm from npm. I found a helpful hint to keep from running "npm install" after I make the change as described here: https://pnpm.js.org/en/only-allow-pnpm

Unfortunately my preinstall lifecycle override doesn't get executed. Seems to simple enough but npm install still works when I run something like "npm install @types/jest"

package.json:

{
  "name": "react-sandbox",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "preinstall": "npx only-allow pnpm"
  }
}

npm version 6.14.2.

Any ideas?

Upvotes: 5

Views: 7462

Answers (2)

karisDev
karisDev

Reputation: 21

You can specify which package manager to use inside package.json

Add the following lines:

  "engines": {
    "node": ">= 18",
    "npm": "Please use pnpm instead of npm to install dependencies",
    "yarn": "Please use pnpm instead of yarn to install dependencies",
    "pnpm": ">= 8"
  }

In this cas npm install will show an error message after the installation, telling the dev to not use npm. It will still try to install dependencies, but no lockfiles or node_modules will appear.

Upvotes: 0

Zoltan Kochan
Zoltan Kochan

Reputation: 7646

Unfortunately, the preinstall script is executed only during argumentless installation. So when you run npm add @types/jest, that script will not be executed, thus npm won't be prevented from running.

But it will fail when running npm install.

As of now, there is no other way to prevent npm from execution.

Upvotes: 7

Related Questions