乐闻世界logo
搜索文章和话题

What the different between `pnpm install` and `pnpm add`?

1个答案

1

pnpm install and pnpm add are two commands in the pnpm package manager. They function similarly in some cases but have significant differences in others:

  1. pnpm install:

    • This command, when used without parameters, is typically used to install or update all dependencies listed in package.json.
    • When you first set up a project or after cloning someone else's project, you can run pnpm install to install all necessary dependencies.
    • pnpm install is also used for globally installing packages by adding the -g flag.
    • If you have previously installed dependencies, pnpm install will update them and maintain consistency with the pnpm-lock.yaml file.
    • This command does not modify the package.json file unless you use specific parameters, such as pnpm install <package-name>.
  2. pnpm add:

    • pnpm add is used to add one or more new dependencies to the project.
    • Running pnpm add <package-name> adds the latest version of the package to the dependencies list in package.json and installs it.
    • You can specify installing a particular version of the package using pnpm add <package-name>@<version>.
    • Similarly, you can add the package as a development dependency by using -D or --save-dev.
    • pnpm add can also be used for globally installing packages by adding the -g flag.
    • In summary, pnpm add is used for adding new dependencies and modifies both the package.json and pnpm-lock.yaml files.

Example:

Suppose we have a new project that needs to add the react library:

  • Using pnpm add react adds react as a dependency in the project's package.json and installs it.
  • If we already have a package.json listing the required dependencies, using pnpm install will install all listed dependencies based on this file.

In summary, pnpm add is used for adding new dependencies, while pnpm install is typically used for installing or updating existing dependencies. In practice, the pnpm add command is commonly used during development when adding new libraries to your project, whereas pnpm install is used when setting up the project initially or when synchronizing dependencies based on the lock file.

2024年6月29日 12:07 回复

你的答案