The difference between npm, pnpm and yarn

Package management tools are an indispensable part of JavaScript development. They can help us easily install, update, delete and manage various libraries and modules that the project depends on.

Currently, the most popular package management tools are npm, yarn and pnpm, each of which has its own characteristics, advantages and disadvantages.

This article will attempt to conduct a comprehensive comparison of these three tools.

npm

npm is the official package management tool for Node.js. It was released with Node.js in 2010 and is one of the earliest JavaScript package management tools.

The goal of npm is to provide a simple, reliable, and efficient way to manage the dependencies of Node.js projects. npm is designed around the idea of ​​semantic versioning (semver), recording a project's metadata and dependencies through a file called package.json. npm also provides an online service called npm registry for storing and distributing public or private packages.

Features

The main features of npm are:

Official support

npm is the official package management tool for Node.js. It works closely with Node.js versions and features, and has also received extensive support and contributions from the Node.js community.

rich ecology

npm has more than 1.6 million public packages, covering all aspects of JavaScript development, and is currently one of the largest package repositories. npm also provides some other services, such as npm audit, npm ci, npm run, etc., to enhance developer experience and security.

Simple and easy to use

The use of npm is very simple. You only need a few basic commands, such as npm init, npm install, npm update, etc., to complete the initialization, installation, update and other operations of the project. npm's documentation is also very complete, providing detailed guides and references.

Usage examples

# 在项目中安装依赖项
npm install lodash
# 全局安装包
npm install -g create-react-app
# 查看已安装的包
npm list
# 清空缓存
npm cache clean

pnpm

pnpm is a new package management tool developed by Zoltan Kochan. It was released in November 2016, but it did not become popular as quickly as yarn.

The main goal of pnpm is to solve the problems of duplication and ghost dependencies caused by the flat installation methods of npm and yarn. That is, a package with the same name will only upgrade one version, and the remaining versions will still be copied multiple times, and some dependencies may can be unexpectedly promoted or missing.

pnpm introduces a global cache directory called store to store all installed packages, and then organizes the node_modules directory through hard links and soft links to ensure that each package can access its own dependencies without accessing Dependencies to other packages.

This ensures version consistency of dependencies while saving disk space and installation time.

Features

The main features of pnpm are:

Efficient and saving

pnpm introduces a global cache directory called store to store all installed packages, and then organizes the node_modules directory through hard links and soft links to ensure that each package can access its own dependencies without accessing Dependencies to other packages. This ensures version consistency of dependencies while saving disk space and installation time.

Version consistent

pnpm locks the version and content of dependencies by introducing a file named pnpm-lock.yaml to ensure that each installation can get the same results and avoid errors and inconsistencies caused by changes in dependencies.

Hard link installation

pnpm installs dependencies by using hard links instead of copying files, which can avoid duplicate dependencies and file path length problems, and can improve the speed and stability of installation.

Usage examples

# 在项目中安装依赖项
pnpm add lodash
# 全局安装包
pnpm add -g create-react-app
# 查看已安装的包
pnpm list
# 清空缓存
pnpm cache clean

yarn

Yarn is a new package management tool jointly developed by Facebook, Google, Expo and other companies. It was released in October 2016 and quickly became popular among developers.

The main goal of yarn is to solve the non-deterministic problem of npm installation, that is, different developers may install different versions of dependencies for the same project, resulting in errors that are difficult to debug. yarn locks the versions and contents of dependencies by introducing a file called yarn.lock, ensuring that you get the same results every installation.

Yarn also uses parallelization, caching, offline and other technologies to improve the speed and stability of installation.

Features

The main features of yarn are:

Fast and stable

yarn improves the speed and stability of installation through parallelization, caching, offline and other technologies. It can complete the installation of large projects in a few seconds without failing due to network or other factors.

predictable

Yarn locks the version and content of dependencies by introducing the yarn.lock file to ensure that each installation can get the same results and avoid errors and inconsistencies caused by changes in dependencies.

Flexible innovation

Yarn abandons the node_modules directory by introducing the Plug'n'Play mode. Instead, it manages the parsing and loading of dependencies through a file named .pnp.js. This can further save disk space and installation time, but it also requires the use of Node. The .js module system undergoes some modifications and adaptations.

Usage examples

# 在项目中安装依赖项
yarn add lodash
# 全局安装包
yarn global add create-react-app
# 查看已安装的包
yarn list
# 清空缓存
yarn cache clean

Compare

Installation speed

  • npm:  Relatively slow, especially in projects with a lot of dependencies.

  • pnpm:  Faster installation, especially in multi-project workspaces.

  • yarn:  uses parallel downloading, which is relatively fast.

The following is a comparison of the performance of three tools executing different commands:

The performance advantages of pnpm are very obvious.

Disk space occupied

  • npm:  By default, dependencies are copied to the project's node_modulesdirectory, which may cause repeated use of disk space.

  • pnpm:  Sharing dependencies through symbolic links reduces disk space usage.

  • yarn:  In offline mode, the disk space usage is reduced through the caching mechanism

Concurrent installation

  • npm:  Concurrent installation is not supported and dependencies will be installed one by one.

  • pnpm:  Supports concurrent installation, improving installation speed.

  • yarn:  uses parallel downloading, which is relatively fast.

stability

  • npm:  stable, mature, and widely used.

  • pnpm:  Relatively new, but the community is growing.

  • yarn:  maintained by Facebook and has high stability.

Which one is better?

Different tools have different advantages and disadvantages and are suitable for different development scenarios and needs.

You can choose the tool that best suits you based on the characteristics and goals of your project, as well as your preferences for speed, stability, compatibility, innovation, etc.

  • If you value the completeness of the ecosystem, choose npm;

  • If you are pursuing faster installation speed and smaller disk footprint, choose pnpm;

  • If you need offline mode and ensure package version consistency, choose yarn.

summary

npmis the default choice, stable and easy to use. pnpmReduce disk space usage by sharing dependencies, suitable for projects that need to optimize space. yarnProvides faster, more reliable installations with parallel downloads and version locking.

As for what to use, it depends on our personal preference. Then there is one thing, it is best not to mix them, otherwise there will be unknown errors.

Guess you like

Origin blog.csdn.net/weixin_42775304/article/details/135369049