pnpm add <pkg>
安装软件包及其依赖的任何软件包。 默认情况下,任何新软件包都安装为生产依赖项。
摘要:
命令 | 释义 |
---|---|
pnpm add sax | 保存到 dependencies |
pnpm add -D sax | 保存到 devDependencies |
pnpm add -O sax | 保存到 optionalDependencies |
pnpm add -g sax | Install package globally |
pnpm add sax@next | 从 next 标签下安装 |
pnpm add sax@3.0.0 | 安装指定版本 3.0.0 |
支持的包地址
从 npm 安装
pnpm add package-name
默认会从 npm registry 安装最新的 package-name
。
如果在 workspace 中执行,该命令将首先去检查这个 worksapce 中的其他项目是否已经使用了这个指定的包。 如果是的话,就使用这个包的版本范围来进行安装。
你还可以通过以下方式安装包:
- tag:
pnpm add express@nightly
- version:
pnpm add express@1.0.0
- version range:
pnpm add express@2 react@">=0.1.0 <0.2.0"
从 workspace 安装
需要注意的是当我们使用 workspace 安装依赖时,会从已配置的源处进行安装,当然取决于是否设置了 link-workspace-packages
,以及是否使用了 workspace: range protocol
。
从本地安装
从本地安装的两种方法:
- 源码文件 (
.tar
,.tar.gz
, or.tgz
) - 本地目录
示例:
pnpm add ./package.tar.gz
pnpm add ./some-directory
当从目录安装时,会在当前项目目录中生成一个 symlink node_modules
, 因此这里跟执行 pnpm link
是一致的.
从远端安装 Tar 包
参数必须是一个可访问的 URL,"http://" 或者 "https://" 开头的。
示例:
pnpm add https://github.com/indexzero/forever/tarball/v0.5.6
从 git 安装
pnpm add <git remote url>
Installs the package from the hosted Git provider, cloning it with Git.
You may install packages from Git by:
- Latest commit from default branch:
pnpm add kevva/is-positive
- Git commit hash:
pnpm add kevva/is-positive#97edff6f525f192a3f83cea1944765f769ae2678
- Git branch:
pnpm add kevva/is-positive#master
- Git branch relative to refs:
pnpm add zkochan/is-negative#heads/canary
- Git tag:
pnpm add zkochan/is-negative#2.0.1
- V-prefixed Git tag:
pnpm add andreineculau/npm-publish-git#v0.0.7
Install from a Git repository using semver
You can specify version (range) to install using the semver:
parameter. 示例:
- Strict semver:
pnpm add zkochan/is-negative#semver:1.0.0
- V-prefixed strict semver:
pnpm add andreineculau/npm-publish-git#semver:v0.0.7
- Semver version range:
pnpm add kevva/is-positive#semver:^2.0.0
- V-prefixed semver version range:
pnpm add andreineculau/npm-publish-git#semver:<=v0.0.7
Install from a subdirectory of a Git repository
You may also install just a subdirectory from a Git-hosted monorepo using the path:
parameter. 例如:
pnpm add RexSkz/test-git-subdir-fetch#path:/packages/simple-react-app
Install from a Git repository via a full URL
If you want to be more explicit or are using alternative Git hosting, you might want to spell out full Git URL:
# git+ssh
pnpm add git+ssh://git@github.com:zkochan/is-negative.git#2.0.1
# https
pnpm add https://github.com/zkochan/is-negative.git#2.0.1
Install from a Git repository using hosting providers shorthand
You can use a protocol shorthand [provier]:
for certain Git providers:
pnpm add github:zkochan/is-negative
pnpm add bitbucket:pnpmjs/git-resolver
pnpm add gitlab:pnpm/git-resolver
If [provider]:
is omited, it defaults to github:
.
Install from a Git repository combining different parameters
It is possible to combine multiple parameters by separating them with &
. This can be useful for forks of monorepos:
pnpm add RexSkz/test-git-subdir-fetch.git#beta&path:/packages/simple-react-app
Installs from the beta
branch and only the subdirectory at /packages/simple-react-app
.