pnpm is an efficient package manager that saves disk space by sharing the same package versions across multiple projects using hard links and symbolic links (symlinks). When you install a package with pnpm, it stores the package contents in a global storage directory and creates symbolic links to these global contents in your project's node_modules directory.
Regarding globally installed packages, pnpm also supports this feature, but its approach differs from npm or yarn. In npm or yarn, globally installed packages are typically placed in a system-wide location, and executable files are added to the system's PATH via symbolic links. However, pnpm avoids global command pollution by employing a unique method: it installs global packages in a dedicated global directory, and creates symbolic links only when you explicitly add the executable files to the PATH.
Here are the steps for using pnpm to install global packages and manage symbolic links in the global directory:
Global Installation of Packages
- Install a package globally:
shellpnpm add -g <package-name>
This installs <package-name> in pnpm's global storage directory and creates symbolic links to the executable files in pnpm's global bin directory.
- View global package location:
To see where pnpm installs global packages, run:
shellpnpm config get pnpmHome
This tells you the global storage location and the global bin directory.
Managing Global Symbolic Links
- List globally installed packages:
shellpnpm ls -g
This lists all globally installed packages.
- Add global bin to PATH:
You need to add pnpm's global bin directory to your PATH environment variable so you can run globally installed package executables directly from the command line. How to add depends on your OS and shell, but typically you add the following line to your shell configuration file (e.g.,.bashrc,.zshrc,.bash_profile, or.profile):
shellexport PATH="$PATH:$(pnpm config get pnpmHome)/bin"
Then reload your shell configuration file, e.g., with source ~/.bashrc.
- Remove global packages:
shellpnpm remove -g <package-name>
This removes <package-name> from the global storage and deletes the corresponding symbolic links.
By doing this, pnpm efficiently manages global commands and packages, reducing disk space usage and simplifying version management.