A Quick Guide to npm, Yarn, and pnpm: Package Managers for JavaScript

When it comes to managing dependencies in JavaScript projects, package managers are essential tools for developers. Three of the most popular options are npm, Yarn, and pnpm. In this post, we’ll briefly introduce each one and provide instructions for setting up the Taobao mirror (a fast alternative registry for users in China) for all three.

What Are npm, Yarn, and pnpm?

npm

npm is the default package manager for Node.js. It’s widely used, comes bundled with Node.js, and has a massive registry of packages. It’s reliable and constantly evolving, making it a go-to choice for many developers.

Yarn

Yarn was created by Facebook to address some of npm’s early shortcomings, such as speed and consistency. It introduced features like lockfiles for deterministic installs and parallel downloads, making it faster and more predictable.

pnpm

pnpm is a newer player designed for efficiency. It uses a unique approach by linking dependencies from a global store rather than duplicating them in every project, saving disk space and speeding up installations.

Setting Up the Taobao Mirror

For developers in China, using the Taobao mirror (npmmirror.com) can significantly speed up package downloads. Below are the commands to configure npm, Yarn, and pnpm to use the Taobao registry, along with how to verify the settings.

npm

1
2
3
4
5
# Set Taobao mirror
npm config set registry https://registry.npmmirror.com/

# Verify the registry
npm config get registry

Yarn

1
2
3
4
5
# Set Taobao mirror
yarn config set registry https://registry.npmmirror.com/

# Verify the registry
yarn config get registry

pnpm

1
2
3
4
5
# Set Taobao mirror
pnpm config set registry https://registry.npmmirror.com/

# Verify the registry
pnpm config get registry

Bonus: Configuring node-sass with Taobao Mirror

If your project uses node-sass, you can also configure it to fetch binaries from the Taobao mirror for faster downloads:

npm

1
npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/

Yarn

1
yarn config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/

pnpm

1
pnpm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/

A Quick Guide to npm, Yarn, and pnpm: Package Managers for JavaScript
https://www.hardyhu.cn/2024/12/23/A-Quick-Guide-to-npm-Yarn-and-pnpm-Package-Managers-for-JavaScript/
Author
John Doe
Posted on
December 23, 2024
Licensed under