pnpm 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

pnpm 命令安装:


-bash/zsh: pnpm: command not found

# Windows
iwr https://get.pnpm.io/install.ps1 -useb | iex
或
scoop install nodejs-lts pnpm

# POSIX systems
curl -fsSL https://get.pnpm.io/install.sh | sh -
或
wget -qO- https://get.pnpm.io/install.sh | sh -

# Alpine
# bash
wget -qO- https://get.pnpm.io/install.sh | ENV="$HOME/.bashrc" SHELL="$(which bash)" bash -
# sh
wget -qO- https://get.pnpm.io/install.sh | ENV="$HOME/.shrc" SHELL="$(which sh)" sh -
# dash
wget -qO- https://get.pnpm.io/install.sh | ENV="$HOME/.dashrc" SHELL="$(which dash)" dash -

# 通过 npm 安装 pnpm
npm install -g pnpm
或
npm install -g @pnpm/exe

# Homebrew
brew install pnpm

# 卸载 pnpm
pnpm rm -g <pkg>
pnpm root -g
...
rm -rf $PNPM_HOME
npm rm -g pnpm

pnpm 命令补充说明:


pnpm 是同类工具速度的将近 2 倍,node_modules 中的所有文件均克隆或硬链接自单一存储位置。

pnpm 内置了对单个源码仓库中包含多个软件包的支持,pnpm 创建的 node_modules 默认并非扁平结构,因此代码无法对任意软件包进行访问。

pnpm 速度慢,使用镜像:


# pnpm 淘宝镜像
pnpm config set registry https://registry.npmmirror.com

# 切回 pnpm 原镜像
pnpm config set registry https://registry.npmjs.org

# 查看当前 pnpm 镜像
pnpm config get registry

pnpm 命令语法:


pnpm [OPTIONS] ...

pnpm 命令选项:


NAME                              DESCRIPTION
===============================================================================================================
add                               Installs a package and any packages that it depends on. By default, any new package is installed as a production dependency

install, i                        Pnpm install is used to install all dependencies for a project. In a CI environment, installation fails if a lockfile is present but needs an update. Inside a workspace, pnpm install installs all dependencies in all the projects. If you want to disable this behavior, set the recursive-install setting to false

install-test, it                  Runs pnpm install followed immediately by pnpm test. It takes exactly the same arguments as pnpm install

update, upgrade, up               Pnpm update updates packages to their latest version based on the specified range. When used without arguments, updates all dependencies. You can use patterns to update specific dependencies

remove, rm, uninstall, un         Removes packages from node_modules and from the project's package.json

link, ln                          Makes the current local package accessible system-wide, or in another location

unlink                            Unlinks a system-wide package (inverse of pnpm link). If called without arguments, all linked dependencies will be unlinked. This is similar to yarn unlink, except pnpm re-installs the dependency after removing the external link

import                            Pnpm import generates a pnpm-lock.yaml from an npm package-lock.json (or npm-shrinkwrap.json) file

rebuild, rb                       Rebuild a package

prune                             Removes unnecessary packages

fetch                             EXPERIMENTAL FEATURE: Fetch packages from a lockfile into virtual store, package manifest is ignored: https://pnpm.io/cli/fetch

patch                             This command will cause a package to be extracted in a temporary directory intended to be editable at will

patch-commit                      Generate a patch out of a directory

audit                             Checks for known security issues with the installed packages. If security issues are found, try to update your dependencies via pnpm update. If a simple update does not fix all the issues, use overrides to force versions that are not vulnerable. For instance, if lodash@<2.1.0 is vulnerable, use overrides to force lodash@^2.1.0. Details at: https://pnpm.io/cli/audit

list, ls                          This command will output all the versions of packages that are installed, as well as their dependencies, in a tree-structure. Positional arguments are name-pattern@version-range identifiers, which will limit the results to only the packages named. For example, pnpm list "babel-*" "eslint-*" semver@5

outdated                          Checks for outdated packages. The check can be limited to a subset of the installed packages by providing arguments (patterns are supported)

why                               Shows all packages that depend on the specified package

run, run-script                   Runs a script defined in the package's manifest file
exec  Execute a shell command in scope of a project. node_modules/.bin is added to the PATH, so pnpm exec allows executing commands of dependencies

test, t, tst                      Runs an arbitrary command specified in the package's test property of its scripts object. The intended usage of the property is to specify a command that runs unit or integration testing for your program

start                             Runs an arbitrary command specified in the package's start property of its scripts object. If no start property is specified on the scripts object, it will attempt to run node server.js as a default, failing if neither are present. The intended usage of the property is to specify a command that starts your program

publish                           Publishes a package to the registry. When publishing a package inside a workspace, the LICENSE file from the root of the workspace is packed with the package (unless the package has a license of its own). You may override some fields before publish, using the publishConfig field in package.json. You also can use the publishConfig.directory to customize the published subdirectory (usually using third party build tools). When running this command recursively (pnpm -r publish), pnpm will publish all the packages that have versions not yet published to the registry

recursive, m, multi, -r           Runs a pnpm command recursively on all subdirectories in the package or every available workspace
server  Manage a store server

store                             Managing the package store

pnpm 命令实例:


pnpm 创建 package.json 文件:

pnpm init

pnpm 下载 package.json 中列为依赖项的所有包:

pnpm install

pnpm 下载特定版本的包并将其添加到 package.json 的依赖项列表中:

pnpm add [module_name]@[version]

pnpm 下载包并将其添加到 package.json 中的 [D]ev 依赖项列表中:

pnpm add -D [module_name]

pnpm 卸载包并将其从 package.json 的依赖项列表中删除

pnpm remove [module_name]

pnpm 打印本地安装的模块树

pnpm list

pnpm 列出顶级[g]全局安装的模块

pnpm list -g --depth=[0]

pnpm 使用中可能遇到的坑:


解决 yarn 或 pnpm 遇到的坑: 无法加载文件 C:\Users\hp\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本:

pnpm : 无法加载文件 C:\Users\HP\AppData\Roaming\npm\pnpm.ps1,
因为在此系统上禁止运行脚本。有关详细信息,
请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。

所在位置 行:1 字符: 1
 - pnpm
 - ~~~~
    + CategoryInfo          : SecurityError: (:) [],PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

解决办法:运行管理员权限 PowerShell,执行:
set-ExecutionPolicy RemoteSigned,选择 [A] 即可。

pnpm 扩展阅读:




pnpm 命令评论