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:
-
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 installto install all necessary dependencies. pnpm installis also used for globally installing packages by adding the-gflag.- If you have previously installed dependencies,
pnpm installwill update them and maintain consistency with thepnpm-lock.yamlfile. - This command does not modify the
package.jsonfile unless you use specific parameters, such aspnpm install <package-name>.
- This command, when used without parameters, is typically used to install or update all dependencies listed in
-
pnpm add:
pnpm addis 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 inpackage.jsonand 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
-Dor--save-dev. pnpm addcan also be used for globally installing packages by adding the-gflag.- In summary,
pnpm addis used for adding new dependencies and modifies both thepackage.jsonandpnpm-lock.yamlfiles.
Example:
Suppose we have a new project that needs to add the react library:
- Using
pnpm add reactaddsreactas a dependency in the project'spackage.jsonand installs it. - If we already have a
package.jsonlisting the required dependencies, usingpnpm installwill 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 回复