LinuxCommandLibrary

pnpm

Manage Node.js package dependencies efficiently

TLDR

Create a package.json file

$ pnpm init
copy

Download all the packages listed as dependencies in package.json
$ pnpm install
copy

Download a specific version of a package and add it to the list of dependencies in package.json
$ pnpm add [module_name]@[version]
copy

Download a package and add it to the list of dev dependencies in package.json
$ pnpm add [[-D|--save-dev]] [module_name]
copy

Download a package and install it globally
$ pnpm add [[-g|--global]] [module_name]
copy

Uninstall a package and remove it from the list of dependencies in package.json
$ pnpm remove [module_name]
copy

Print a tree of locally installed modules
$ pnpm list
copy

List top-level globally installed modules
$ pnpm list [[-g|--global]] --depth [0]
copy

SYNOPSIS

pnpm <command> [<flags>]
Examples:
pnpm install
pnpm add <package>
pnpm run <script>

PARAMETERS

--filter
    Apply the command to specific packages within a workspace or monorepo.

--global, -g
    Install a package globally on the system.

--offline
    Perform installation using only packages available in the local cache, without network access.

--recursive, -r
    Run a command in every package of the current workspace or monorepo.

--shamefully-hoist
    Create a flat node_modules structure, similar to npm or Yarn Classic, which might be necessary for compatibility with some older tools or ecosystems.

--dir
    Specify the root directory of the project where pnpm should operate.

DESCRIPTION

pnpm (performant npm) is a modern, fast, and disk-efficient package manager for Node.js. It aims to overcome limitations of traditional package managers like npm and Yarn by employing a unique linking strategy. Instead of copying packages, pnpm uses a content-addressable store on disk to store all package versions. When installing dependencies for a project, it creates hard links from this global store to the project's node_modules directory, and then uses symlinks to create the project's dependency tree. This approach significantly reduces disk space consumption, especially in monorepos or across multiple projects sharing dependencies. Additionally, pnpm creates a strict node_modules structure, preventing accidental access to undeclared dependencies, which improves build reliability and helps catch issues early. It offers a command-line interface similar to npm, making it familiar for users, while providing superior performance and efficiency.

CAVEATS

pnpm's strict node_modules structure, while beneficial for reliability, can sometimes cause compatibility issues with older tools or build systems that implicitly rely on hoisted (undeclared) dependencies. The --shamefully-hoist flag can be used as a workaround but should be considered a temporary solution. Users migrating from npm or Yarn might need to adjust their workflow slightly to accommodate pnpm's stricter dependency resolution.

CONTENT-ADDRESSABLE STORE

pnpm maintains a global content-addressable store on your system. This means that each unique version of a package is stored only once. When a project needs a package, pnpm uses hard links from this store to the project's node_modules, dramatically saving disk space.

MONOREPO SUPPORT

pnpm excels in monorepo environments. Its workspace feature allows managing multiple projects within a single repository, and its efficient linking mechanism ensures that dependencies shared across packages are only stored once, leading to faster installations and reduced disk footprint for the entire monorepo.

STRICTNESS AND DETERMINISM

Unlike traditional package managers that might flatten the node_modules structure, pnpm creates a strict, symlinked structure. This ensures that a project can only access dependencies explicitly declared in its package.json, leading to more deterministic builds and preventing "phantom dependencies" issues.

HISTORY

pnpm was created by Zoltan Kochan and first released in 2016. Its primary motivation was to address the inefficiencies of disk space usage and installation times prevalent in Node.js package management. It quickly gained traction, particularly among users managing large monorepos or projects with numerous dependencies, due to its innovative hard-linking approach and strong performance benefits. Its adoption has steadily grown as developers increasingly prioritize faster builds and more efficient resource utilization.

SEE ALSO

npm(1), yarn(1), node(1)

Copied to clipboard