Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Yarn install from running in project (i.e. force NPM install)

We recently switched back to using NPM from Yarn, but old habits die hard and I'm worried some devs will accidentally use yarn install.

How can I prevent yarn install from being run in the project? Or, even better, display a reminder to use npm install?

I'm thinking the yarn install can be intercepted with a preinstall script, but I'm not sure what to look for in the preinstall script.

like image 775
Brett DeWoody Avatar asked Aug 02 '19 14:08

Brett DeWoody


People also ask

Can I use Yarn install instead of npm install?

Yarn can consume the same package. json format as npm, and can install any package from the npm registry. Show activity on this post. First of all Yarn is a package manager created by Facebook as an alternative to npm.

What is Yarn install force?

yarn install is used to install all dependencies for a project. This is most commonly used when you have just checked out code for a project, or when another developer on the project has added a new dependency that you need to pick up.

How do I stop npm from installing?

You can stop the process on the console like any other process: Ctrl + c .

How force npm install?

The -f or --force argument will force npm to fetch remote resources even if a local copy exists on disk. The -g or --global argument will cause npm to install the package globally rather than locally.


Video Answer


1 Answers

You can see whether it's Yarn or NPM running by looking at the value of the npm_execpath environment variable. If you did something like:

"preinstall": "if [[ $npm_execpath =~ 'yarn' ]]; then echo 'Use NPM!' && exit 1; fi",

Then yarn install (or just yarn) would fail prior to the install step. If you want to make this cross-platform or you're not using *nix, then you could write a simple script like:

#! /usr/bin/env node

if (process.env.npm_execpath.match(/yarn/)) {
  console.log("Use NPM!");
  process.exit(1);
}

and run that in preinstall.

like image 135
jonrsharpe Avatar answered Oct 28 '22 02:10

jonrsharpe