Overview
The lecture compares npm and pnpm, highlighting pnpm’s innovative approach to package management, performance, disk usage, and peer dependency handling in JavaScript development.
npm vs. pnpm: Directory Structure
- npm uses a flattened node_modules directory starting from version 3, which simplifies but may cause dependency issues.
- pnpm uses symbolic (sym) and hard links to manage packages, enhancing organization and efficiency.
- Hard links point directly to file inodes, while sym links reference the original file.
How pnpm Manages Dependencies
- pnpm creates hard links to globally stored packages, avoiding file duplication across projects.
- Sym links are used to maintain the correct structure within individual projects.
- This method provides strict control, faster installs, and less disk space usage.
Handling Peer Dependencies in pnpm
- Peer dependencies are hoisted to a unified .pnpm/node_modules directory.
- pnpm scans the node_modules chain upward to resolve compatible peer dependency versions.
- Sharing peer dependencies through hard links reduces duplication and increases project efficiency.
Performance & Efficiency
- pnpm’s linking approach enables faster installs and updates than npm’s copy-paste method.
- Disk space is efficiently used by sharing packages globally, minimizing data duplication.
Installing and Using pnpm
- To install pnpm globally (with npm and node.js installed), use:
npm install -g pnpm.
- Without npm, pnpm can be installed via other command-line instructions.
- Install packages using
pnpm add <package> instead of npm i.
- Run scripts from package.json using
pnpm <script>.
Limitations of pnpm
- pnpm ignores npm lock files (npm-shrinkwrap.json, package-lock.json) due to its different directory structure.
- The
pnpm import command can generate a pnpm-lock.yaml from existing npm lock files.
Key Terms & Definitions
- npm (Node Package Manager) — Default JavaScript package manager, uses a flattened node_modules directory.
- pnpm (Performant npm) — Alternative package manager using hard and sym links for efficient package storage.
- Hard Link — Direct reference to a file's inode, pointing to file data on disk.
- Symbolic Link (Sym Link) — Reference pointing to another file by name.
- Peer Dependency — A dependency required by a package’s host project, not installed automatically.
- Hoisting — Moving dependencies to a higher-level directory for easier resolution.
Action Items / Next Steps
- Try installing pnpm using
npm install -g pnpm.
- Use
pnpm add <package> to install packages in a test project.
- Run
pnpm import if migrating from an npm project to generate a pnpm-lock.yaml file.
- Consult pnpm documentation for advanced features and usage.