Published on

pnpm install vs pnpm add: Key Differences Explained

Authors
  • Name
    Ripal & Zalak
    Twitter

pnpm install vs pnpm add: Key Differences Explained

When working with pnpm, a fast and efficient package manager, you may come across two commonly used commands: pnpm install and pnpm add. While these commands might seem similar at first glance, they serve distinct purposes in dependency management. This guide explores their differences and when to use each.


What Does pnpm install Do?

The pnpm install command is primarily used to:

  1. Install All Dependencies:

    • If run without arguments, it installs all dependencies listed in the pnpm-lock.yaml file (or package.json if no lockfile is present).
    • Example:
      pnpm install
      
  2. Reinstall Dependencies:

    • It ensures all dependencies in the lockfile are installed, making it useful for setting up a project in a fresh environment.
  3. Install Specific Packages:

    • When provided with a package name, it behaves like pnpm add and installs the specified package.
    • Example:
      pnpm install lodash
      
  4. Offline Installation:

    • With the --offline flag, pnpm install installs dependencies from the local cache without connecting to the internet.
    • Example:
      pnpm install --offline
      

Common Use Case

  • Use pnpm install when you want to install all dependencies in an existing project or set up a project from its lockfile.

What Does pnpm add Do?

The pnpm add command is specifically designed to:

  1. Add New Dependencies:

    • It installs a package and adds it to your package.json file (under dependencies by default).
    • Example:
      pnpm add axios
      
  2. Scoped Installation:

    • You can specify a particular scope, such as --save-dev to add a package as a development dependency or --save-peer for peer dependencies.
    • Example:
      pnpm add jest --save-dev
      
  3. Workspace Support:

    • The --workspace flag allows you to add dependencies to specific workspaces within a monorepo.
    • Example:
      pnpm add react --workspace=my-app
      

Common Use Case

  • Use pnpm add when you want to install a new package and automatically update your package.json file.

Key Differences Between pnpm install and pnpm add

Featurepnpm installpnpm add
Primary UseInstalls all dependencies from the lockfile.Installs a new dependency.
Argument RequirementOptional. Works without arguments.Requires a package name.
Modifies package.jsonNo (unless adding a specific package).Yes, updates dependencies or devDependencies.
Workspace FlagNot specific to workspaces.Supports --workspace for monorepos.
Offline ModeSupported via --offline.Not commonly used for offline installs.
Lockfile ReinstallationReinstalls based on the lockfile.Not applicable.

Frequently Asked Questions

1. Can pnpm install and pnpm add be used interchangeably?

In some cases, yes. For example, pnpm install lodash is equivalent to pnpm add lodash. However, pnpm install is generally used for existing projects, while pnpm add is the preferred command for adding new dependencies.

2. Why does pnpm install work without arguments?

When no arguments are provided, pnpm install reads the lockfile (pnpm-lock.yaml) and installs all dependencies specified there. It ensures consistency across environments.

3. Which command should I use in a monorepo?

  • Use pnpm add with the --workspace flag to add dependencies to specific workspaces.
  • Use pnpm install to install all dependencies across the monorepo based on the lockfile.

4. Is there any performance difference?

pnpm add performs additional updates to the package.json file, which can be slightly slower than pnpm install when installing from the lockfile.


Conclusion

  • Use pnpm install for installing all dependencies in an existing project or setting up a project from scratch.
  • Use pnpm add for adding new dependencies and updating the package.json file.

By understanding the differences, you can use the right command for the right task, improving efficiency and consistency in your projects.