Getting Started with Pnpm: Fundamental Commands

Getting Started with Pnpm: Fundamental Commands

pnpm 101 : init, add, install, update, remove, and run commands

As developers, we are constantly looking for tools that can help us to improve our daily work and our productivity. With web projects, package management has been for a long time associated with npm. However, a newer contender, pnpm, offers many benefits that make it worth considering. In this article, we will explore the basic commands of pnpm.

Initializing a new project

pnpm init

This command creates a package.json file where each of the project's dependencies and scripts will be referenced.

Adding a package

pnpm add <package-name>

This command simply adds a new package to your project.

One of the main pnpm's benefits over npm or yarn is its disk space efficiency. While npm installs each package separately for every project, pnpm uses a different approach: packages are stored in a central location on the disk and shared across projects, resulting in significant disk space savings and faster installations.

Installing all dependencies

pnpm install

Run this command to install all dependencies for a project.

Updating packages

pnpm update <package-name>

Just like any package manager, this command allows you to specify a specific package version to update or update all packages in your project. With pnpm's shared store, updating packages becomes faster, as it can reuse packages already present on the disk.

Removing packages

pnpm remove <package-name>

To remove a package from your project, just run this command and specify the name of the package to be removed. Note that if you don't specify the -g option, your package will remain on your disk, ready to be reused within another project.

Running scripts

pnpm run <script-name>
pnpm <script-name>

Defining scripts within the package.json file allows you to have aliases that are easier to manipulate than excessively long CLI commands. You can run them by using the run command and the name of the script. One cool thing is that you can save a few keystrokes by omitting the run keyword, pnpm will automatically look for script aliases.