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.
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.
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.
You can stop the process on the console like any other process: Ctrl + c .
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With