DEV Community

Leonardo Faria
Leonardo Faria

Posted on • Originally published at leonardofaria.net on

3

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

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"
},

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay