npm, Yarn, pnpm: Which Frontend Package Manager Should You Start With?
Frontend Package Management Tools: npm, Yarn, pnpm Quick Start
One-sentence understanding: npm, Yarn, and pnpm are all JavaScript package management tools, used to install, manage, and update project dependencies, and to run project scripts.
If you have Go development experience, you can compare it like this:
| Go Development | Frontend Development |
|---|---|
go get |
npm install |
go.mod |
package.json |
go.sum |
package-lock.json |
| Go Module | npm Package |
go run |
npm run (run predefined scripts) |
1. Why Do We Need Package Management Tools?
Suppose you are developing a React task management platform and need to use:
- React: build user interfaces
- Ant Design: UI component library
- Axios: send HTTP requests
- TypeScript: type checking
- Vite: build tool
Without a package management tool, you would need to download and manage these libraries and their dependencies yourself.
With npm, you just need:
npm install react react-dom
npm install antd axios
npm install -D typescript vite
The package management tool will parse, download, and install the required packages based on the dependency declarations.
2. What Are the Differences Between npm, Yarn, and pnpm?
| Comparison | npm | Yarn | pnpm |
|---|---|---|---|
| Development background | npm, Inc., now part of GitHub | Initiated by Meta (formerly Facebook) | Community open source |
| Included with Node.js | Yes | No | No |
| Common lock file | package-lock.json |
yarn.lock |
pnpm-lock.yaml |
| Dependency storage | Regular node_modules | Supports multiple installation modes | Content-addressed storage, linked reuse |
| Learning curve | Lowest | Requires understanding of version configuration | Relatively low |
| Suitable scenarios | General projects, beginners | Projects already using Yarn | Monorepo, large projects |
For your current learning stage, mastering npm is sufficient. The three have similar core functionality; there is no need to learn them all in depth.
3. The Most Important package.json in Actual Projects
Run:
npm init -y
This creates package.json.
For example:
{
"name": "task-platform",
"version": "1.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint ."
},
"dependencies": {
"react": "^19.0.0",
"react-dom": "^19.0.0",
"antd": "^5.0.0"
},
"devDependencies": {
"vite": "^7.0.0",
"typescript": "^5.0.0"
}
}
The version numbers are only used to demonstrate the structure of package.json and do not represent the latest versions or a recommended combination.
The three most important fields:
| Field | Meaning |
|---|---|
scripts |
Defines project execution commands |
dependencies |
Direct dependencies required for project runtime |
devDependencies |
Direct dependencies needed during development, checking, and building |
Note: devDependencies does not mean the tools cannot be used in the build artifact. For example, Vite is a development dependency, but it is still needed during production builds.
dependencies and devDependencies
# Install a regular dependency
npm install antd
# Install a development dependency
npm install -D typescript
Typically:
antd: UI component library used in business code.typescript: Type checking tool used during development and building.
4. Most Commonly Used npm Commands
This table is recommended to save; it is basically enough for work.
| Command | Purpose |
|---|---|
npm init -y |
Initialize the project |
npm install |
Install all project dependencies |
npm install axios |
Install a specific dependency |
npm install -D vite |
Install a development dependency |
npm uninstall axios |
Uninstall a dependency |
npm run dev |
Run the dev script |
npm run build |
Run the build script |
npm list |
View installed dependencies |
npm ci |
Clean install based on lock file |
Among these, npm ci is especially suitable for CI/CD, because it installs strictly according to the lock file and does not proactively update dependency declarations or the lock file.
npm Docs+1
5. What Are node_modules and Lock Files?
Run:
npm install
After that, a project typically contains:
task-platform/
├── node_modules/
├── package.json
├── package-lock.json
└── src/
These three things need to be distinguished:
package.json: declares which dependencies the project needs and the allowed version ranges.package-lock.json: records the exact resolved dependency versions and the dependency tree, helping different environments get consistent install results.node_modules/: the local directory where dependency files are actually installed.
In general, package-lock.json should be committed to Git, and node_modules/ should not be committed.
6. Command Comparison for npm, Yarn, and pnpm
| Operation | npm | Yarn | pnpm |
|---|---|---|---|
| Install all dependencies | npm install |
yarn install |
pnpm install |
| Add a dependency | npm install axios |
yarn add axios |
pnpm add axios |
| Add a dev dependency | npm install -D vite |
yarn add -D vite |
pnpm add -D vite |
| Remove a dependency | npm uninstall axios |
yarn remove axios |
pnpm remove axios |
| Run a script | npm run dev |
yarn dev |
pnpm dev |
When actually taking over a project, it is best to stick with the package management tool already used by the project, and avoid mixing npm, Yarn, and pnpm arbitrarily.
For example, seeing pnpm-lock.yaml typically means the project uses pnpm; seeing yarn.lock typically means the project uses Yarn.