Codementor Events

Forcing the usage of yarn (and at a specific version)

Published Oct 11, 2019

Also published in my blog.

People and organizations often have preferences for a specific package manager. At work, we decided to use Yarn due to emoji support (jk) but how to keep everybody using Yarn?

We can use the preinstall hook to check if the user run npm installor yarn install. Here is one example:

"scripts": { 
  "preinstall": "node -e \"if(process.env.npm\_execpath.indexOf('yarn') === -1) throw new Error('You must use Yarn to install, not NPM')\"",
}

If you run npm install:

If you want to ignore the checking (CI environment for instance), use the --ignore-scripts option:

npm install --ignore-scripts

Moreover, you can use the engines option of NPM to force a specific version of Node, and/or Yarn. Here is an example:

"engines": { 
  "yarn": ">1.19.1", 
  "node": ">12"
},

Discover and read more posts from Leonardo Faria
get started