跪拜 Guibai
← All articles
Frontend

npm, Yarn, pnpm: Which Frontend Package Manager Should You Start With?

By Kevin666 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Lockfiles tell you which package manager a project actually uses, and mixing npm, Yarn, or pnpm on the same repo can silently diverge dependency resolution. Newcomers get a concrete path from Go conventions to npm commands, plus a simple rule to follow the existing lockfile instead of switching tools.

Summary

npm, Yarn, and pnpm all solve the same problem: installing and managing JavaScript dependencies and running project scripts. The guide maps each concept to Go equivalents (go get to npm install, go.mod to package.json, go.sum to package-lock.json), lowering the barrier for developers coming from the Go ecosystem. It then lays out the core differences in storage, lockfiles, and typical use cases, while explicitly recommending that newcomers master npm first because the three share the same core functionality.

package.json sits at the center of the workflow. The guide explains the roles of the scripts, dependencies, and devDependencies fields, and shows how commands like npm install and npm install -D fill them. A reference table covers everyday commands, including npm ci for clean, lockfile-exact installs suitable for CI/CD. The distinction between package.json, package-lock.json, and node_modules is made plain: the lockfile records exact versions and should be committed to Git, while node_modules is local and should not.

A final command comparison across npm, Yarn, and pnpm gives the exact syntax for install, add, remove, and run operations. The closing advice matters for real projects: when you inherit a codebase, detect its package manager from the lockfile (pnpm-lock.yaml, yarn.lock, or package-lock.json) and stick with it rather than mixing tools.

Takeaways
— npm, Yarn, and pnpm all install, manage, and update JavaScript dependencies and run project scripts.
— npm ships with Node.js; Yarn and pnpm do not.
— Common lockfiles are package-lock.json (npm), yarn.lock (Yarn), and pnpm-lock.yaml (pnpm).
— package.json has three key fields: scripts, dependencies, and devDependencies.
— devDependencies are still used during production builds; Vite is a dev dependency but needed at build time.
— npm ci installs strictly from the lockfile without updating anything, making it ideal for CI/CD.
— package-lock.json should be committed to Git; node_modules/ should not be committed.
— When working on an existing project, keep using the package manager its lockfile indicates and avoid mixing npm, Yarn, and pnpm.
Conclusions

The Go-to-frontend mapping gives a developer who already knows go get, go.mod, and go.sum a nearly frictionless on-ramp to npm, which is why the guide is structured around it.

Recommending npm as the sole tool to learn first acknowledges that the three managers are functionally equivalent for typical projects, so parallel learning only adds cognitive load without practical payoff.

The lockfile-as-identifier heuristic is a practical way to avoid tool-switching damage; seeing pnpm-lock.yaml or yarn.lock is enough to know which manager a repo expects.

Concepts & terms
package-lock.json
A lockfile generated by npm that records the exact resolved version of every dependency and the full dependency tree, ensuring consistent installs across different environments.
node_modules
The local directory where installed dependency files are physically stored. It is generated by package managers and typically excluded from version control.
devDependencies
A package.json section listing dependencies needed only for development, checking, and building. These tools can still be required during production builds, as with Vite.
npm ci
A npm command that performs a clean install strictly from package-lock.json without modifying it, making it reliable for CI/CD pipelines.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗