💻 Programming

pnpm

pnpm

A fast, disk-efficient Node.js package manager. Uses hard links.

📖 Detailed Explanation

pnpm (performant npm) is, along with npm and Yarn, one of the major package managers in the Node.js ecosystem. Its most notable feature is a distinctive storage method that uses hard links and symbolic links, which dramatically saves disk space.

pnpm stores a package only once in a global store, and each project links to it with hard links. For example, even if 10 projects use the same version of lodash, it is actually downloaded only once. This is especially effective in large monorepos.

In addition, pnpm uses a strict node_modules structure. A package can access only its own dependencies, which prevents the phantom dependencies problem. This prevents you from accidentally using a package that is not declared in package.json.

Installation is also fast. Thanks to a content-addressable store and parallel installation, installs can be up to 2 times faster or more than npm. It also has a built-in workspace feature, so it is optimized for monorepo management.

💻 Code Example

Bash
# pnpm 설치
npm install -g pnpm

# 프로젝트 의존성 설치
pnpm install

# 패키지 추가
pnpm add express
pnpm add -D typescript  # devDependencies로 추가

# 워크스페이스에서 특정 패키지에 설치
pnpm add lodash --filter @myorg/api

# 스크립트 실행
pnpm run build
pnpm test

# 모든 워크스페이스에서 명령 실행
pnpm -r run build  # recursive

# 패키지 업데이트
pnpm update
pnpm update --interactive  # 대화형 업데이트

# 불필요한 패키지 정리
pnpm prune

# 저장소 크기 확인
pnpm store status

🗣️ Examples of Practical Conversations

DevOps engineer
"npm install is taking too long in our CI pipeline. Even with node_modules cached, it takes more than 5 minutes."
Senior developer
"How about migrating to pnpm? The cache efficiency is much better, and it saves disk space, so managing the build server will also become easier."
DevOps engineer
"Sounds good. We were also planning to set up a monorepo, and since pnpm has a workspace feature, we could do it without Lerna."
Interviewer
"Please explain the differences between npm, Yarn, and pnpm, and in which situations you would choose each one."
Candidate
"npm is the default and has good compatibility. Yarn's strengths are workspaces and PnP mode, and pnpm is characterized by disk efficiency and strict dependency management. For a monorepo, I prefer pnpm."
Interviewer
"What problems does pnpm's strict mode prevent?"
Candidate
"It prevents the phantom dependencies problem. If you pull in a package that isn't declared in package.json from a sub-dependency, a runtime error occurs when its version changes or it is removed, and pnpm blocks this at install time."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

Reviewer
"There's a package-lock.json, but there's also a pnpm-lock.yaml. We should unify them into one."
Author
"Ah, that's left over from migrating from npm. I'll delete package-lock.json and add the package manager settings to .npmrc."
Reviewer
"Please also add the packageManager field to package.json. Corepack will automatically match the pnpm version."

⚠️ Cautions

🔗 Related Terms

📚 Learn More