pnpm add <pkg>
安裝一個套件與其所有依賴的套件。 預設狀況下,新安裝的套件將被視為生產環境依賴套件安裝。
範例#
| 命令 | 效果 |
|---|---|
pnpm add sax | 安裝該套件至 dependencies |
pnpm add -D sax | 安裝該套件至 devDependencies |
pnpm add -O sax | 安裝該套件至 optionalDependencies |
pnpm add -g sax | 全域安裝該套件 |
pnpm add sax@next | 安裝標籤為 next 的套件 |
pnpm add sax@3.0.0 | 安裝版本為 3.0.0 的套件 |
支援的套件來源#
pnpm 支援從多種來源安裝套件。 參見 支援的套件來源 頁面以獲得更多資訊:
- npm 登錄檔
- JSR 登錄檔
- 工作區套件
- 本地檔案系統(tarball 與目錄)
- 遠端的 tarballs
- Git 倉庫(包含語義化版本、子目錄等)
Adding a package manager or a runtime#
Added in: v12.0.0-rc.6 (pnpm v12 only)
Naming a package manager — npm, yarn or bun — records which one the project uses instead of installing the npm package that shares the name:
pnpm add yarn@4
writes "packageManager": "yarn@4.18.0", and every other package manager is recorded as a range in devEngines.packageManager. Naming a runtime (node, deno) records it under engines.runtime, as the explicit node@runtime:22 spelling already did — bun is both, and is declared as the project's package manager unless you ask for the runtime (pnpm add bun@runtime:1.3.0).
Globally, pnpm add -g yarn installs the current Yarn line rather than the Classic-only yarn package, and pnpm add -g node@22 installs that Node.js release rather than a wrapper that downloads one.
A specifier that locates a package rather than asking for a released version — pnpm add yarn@npm:yarn@1.22.22, pnpm add yarn@yarnpkg/berry — installs what it names, as an ordinary dependency.
參數#
--save-prod, -P, -p#
安裝套件至 dependencies
--save-dev, -D, -d#
安裝套件至 devDependencies
--save-optional, -O, -o#
安裝套件至 optionalDependencies
--save-exact, -E, -e#
安裝套件並鎖定為指定版本,而不會使用 pnpm 預設的 semver 範圍
--save-peer#
使用 --save-peer 將會新增一個或多個套件至 peerDependencies 並且將它們當作開發依賴套件進行安裝。
--save-catalog#
新增於 v10.12.1
將新的依賴套件儲存到預設 目錄 中。
--save-catalog-name <catalog_name>#
新增於 v10.12.1
將新的依賴套件儲存至指定 目錄 中。
--config#
新增於 v10.8.0
將依賴儲存至 configDependencies。
--ignore-workspace-root-check#
除非加上 --ignore-workspace-root-check 或 -w 參數,否則在工作區的根目錄中新增依賴套件時將會失敗。
例如:pnpm add debug -w。
--global, -g#
Install a package globally. See Global Packages for details.
Each space-separated package is installed into its own isolated directory. To bundle several packages into a single isolated install (so they share dependencies and are removed together), pass them as a comma-separated list, e.g. pnpm add -g eslint,prettier.
--workspace#
僅新增可在工作區找到的新套件。
--allow-build#
新增於 v10.4.0
一份允許在安裝期間執行安裝後腳本的套件清單。
例如:
pnpm --allow-build=esbuild add my-bundler
This will run esbuild's postinstall script and also add it to the allowBuilds field of pnpm-workspace.yaml. 因此,esbuild 將來總是被允許執行腳本。
--filter <package_selector>#
--cpu=<name>#
新增於 v10.14.0
Override CPU architecture of native modules to install. Acceptable values are same as cpu field of package.json, which comes from process.arch.
--os=<name>#
新增於 v10.14.0
Override OS of native modules to install. Acceptable values are same as os field of package.json, which comes from process.platform.
--libc=<name>#
新增於 v10.14.0
Override libc of native modules to install. Acceptable values are same as libc field of package.json.