- 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.
pnpm install
Do?
What Does The pnpm install
command is primarily used to:
Install All Dependencies:
- If run without arguments, it installs all dependencies listed in the
pnpm-lock.yaml
file (orpackage.json
if 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 add
and installs the specified package. - Example:
pnpm install lodash
- When provided with a package name, it behaves like
Offline Installation:
- With the
--offline
flag,pnpm install
installs dependencies from the local cache without connecting to the internet. - Example:
pnpm install --offline
- With the
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.
pnpm add
Do?
What Does The pnpm add
command is specifically designed to:
Add New Dependencies:
- It installs a package and adds it to your
package.json
file (underdependencies
by 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-dev
to add a package as a development dependency or--save-peer
for peer dependencies. - Example:
pnpm add jest --save-dev
- You can specify a particular scope, such as
Workspace Support:
- The
--workspace
flag allows you to add dependencies to specific workspaces within a monorepo. - Example:
pnpm add react --workspace=my-app
- The
Common Use Case
- Use
pnpm add
when you want to install a new package and automatically update yourpackage.json
file.
pnpm install
and pnpm add
Key Differences Between 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
pnpm install
and pnpm add
be used interchangeably?
1. Can 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.
pnpm install
work without arguments?
2. Why does 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 thepackage.json
file.
By understanding the differences, you can use the right command for the right task, improving efficiency and consistency in your projects.