A few years ago we switched projects from npm to yarn and in the near future, we will move to pnpm for several reasons (which will be explained in another blog post). Often we have to adapt the scripts in
package.json to use the correct package manager.
To make this easier, we are testing a few solutions so that the developers do not have to take care of using the correct package manager.
Instead of writing
npm run or
yarn run in the
scripts section, we can use
$npm_execpath run, which will use the binary of the current package manager used in the terminal. This is a very simple solution, but this requires a POSIX shell and does not ensure that every team member uses the same package manager.
Node.js 14.10 and 16.9 introduced corepack which makes it easier to ensure that every team member uses the same package manager and version. To set the package manager for a project, you can now use an entry like
"packageManager": "firstname.lastname@example.org+sha224.953c8233f7a92884eee2de69a1b92d1f2ec1655e66d08071ba9a02fa" in
package.json. You don't even have to install the defined package manager, Node.js will handle the automatic installation of it. For the time being corepack has to be manually enabled.
We will start with the simplest approach (
$npm_execpath run) combined with the new corepack feature and the
packageManager field to keep the number of dependencies as low as possible.