- Published on
pnpm install vs pnpm add: Key Differences Explained
- Authors
- Name
- Ripal & Zalak
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:
Install All Dependencies:
- If run without arguments, it installs all dependencies listed in the
pnpm-lock.yamlfile (orpackage.jsonif no lockfile is present). - Example:
pnpm install
- If run without arguments, it installs all dependencies listed in the
Reinstall Dependencies:
- It ensures all dependencies in the lockfile are installed, making it useful for setting up a project in a fresh environment.
Install Specific Packages:
- When provided with a package name, it behaves like
pnpm addand installs the specified package. - Example:
pnpm install lodash
- When provided with a package name, it behaves like
Offline Installation:
- With the
--offlineflag,pnpm installinstalls dependencies from the local cache without connecting to the internet. - Example:
pnpm install --offline
- With the
Common Use Case
- Use
pnpm installwhen 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:
Add New Dependencies:
- It installs a package and adds it to your
package.jsonfile (underdependenciesby default). - Example:
pnpm add axios
- It installs a package and adds it to your
Scoped Installation:
- You can specify a particular scope, such as
--save-devto add a package as a development dependency or--save-peerfor peer dependencies. - Example:
pnpm add jest --save-dev
- You can specify a particular scope, such as
Workspace Support:
- The
--workspaceflag allows you to add dependencies to specific workspaces within a monorepo. - Example:
pnpm add react --workspace=my-app
- The
Common Use Case
- Use
pnpm addwhen you want to install a new package and automatically update yourpackage.jsonfile.
Key Differences Between pnpm install and pnpm add
| Feature | pnpm install | pnpm add |
|---|---|---|
| Primary Use | Installs all dependencies from the lockfile. | Installs a new dependency. |
| Argument Requirement | Optional. Works without arguments. | Requires a package name. |
Modifies package.json | No (unless adding a specific package). | Yes, updates dependencies or devDependencies. |
| Workspace Flag | Not specific to workspaces. | Supports --workspace for monorepos. |
| Offline Mode | Supported via --offline. | Not commonly used for offline installs. |
| Lockfile Reinstallation | Reinstalls 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 addwith the--workspaceflag to add dependencies to specific workspaces. - Use
pnpm installto 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 installfor installing all dependencies in an existing project or setting up a project from scratch. - Use
pnpm addfor adding new dependencies and updating thepackage.jsonfile.
By understanding the differences, you can use the right command for the right task, improving efficiency and consistency in your projects.
