Pnpm别名

Published on
Published on
/3 mins read/---

安装和升级 pnpm

我推荐使用 npm 全局安装 pnpm

npm i -g pnpm

要升级到最新版本,运行:

npm i -g pnpm@latest

在shell中为 pnpm 添加别名

打开你的shell配置文件(例如 .zprofile.bash_profile):

$ code ~/.zprofile
# 或者 code ~/.bash_profile

或者如果你喜欢使用 Vim

$ vim ~/.zprofile
# 或者 vim ~/.bash_profile

在你的shell配置文件中添加以下行:

alias pn=pnpm

然后重新加载配置:

source ~/.zprofile
# 或者 source ~/.bash_profile

现在你可以在shell中使用 pn 来代替 pnpm

$ pn -v
9.12.1

将项目中的依赖升级到最新版本

如果你想将项目中的所有依赖升级到最新版本,可以使用以下命令:

pnpm up -i -L

这将显示将要升级的包列表,并要求你确认升级。

$ pn up -i -L
? Choose which packages to update (Press <space> to select, <a> to toggle all, <i> to invert selection) …
 devDependencies
 Package                                                    Current   Target
 @commitlint/cli                                             19.4.1 19.5.0
 @commitlint/config-conventional                             19.4.1 19.5.0
 @types/node                                                 22.5.4 22.7.5
 @types/react                                                18.3.5 18.3.11
 @typescript-eslint/eslint-plugin                            6.21.0 8.8.1
 @typescript-eslint/parser                                   6.21.0 8.8.1
 eslint                                                      8.57.0 9.12.0
 eslint-config-next                                          14.2.8 14.2.15
 eslint-config-prettier                                      8.10.0 9.1.0
 husky                                                        9.1.5 9.1.6
 prettier-plugin-tailwindcss                                  0.6.6 0.6.8
 typescript                                                   5.5.4 5.6.3
 dependencies
 Package                                                    Current   Target
 @headlessui/react                                            2.1.5 2.1.10
 @next/bundle-analyzer                                       14.2.8 14.2.15
 contentlayer2                                                0.5.0 0.5.1
 esbuild                                                     0.23.1 0.24.0
 lucide-react                                               0.439.0 0.451.0
 next                                                        14.2.8 14.2.15
 next-contentlayer2                                           0.5.0 0.5.1
 pliny                                                        0.3.1 0.3.2
 postcss                                                     8.4.45 8.4.47
 rehype-citation                                              2.1.1 2.2.0
 rehype-preset-minify                                         7.0.0 7.0.1
 tailwindcss                                                 3.4.10 3.4.13
 
Enter to start updating. Ctrl-c to cancel.

命令参数说明

  • -i--interactive: 交互式升级,让你选择要升级的包
  • -L--latest: 升级到最新版本(忽略semver范围)

其他有用的pnpm命令别名

你可以在shell配置文件中添加更多有用的别名:

# 基础别名
alias pn=pnpm
alias pni="pnpm install"
alias pna="pnpm add"
alias pnd="pnpm add -D"
alias pnr="pnpm remove"
alias pnx="pnpm dlx"
 
# 脚本运行
alias pns="pnpm start"
alias pnb="pnpm build"
alias pnt="pnpm test"
alias pndev="pnpm dev"
 
# 依赖管理
alias pnu="pnpm update"
alias pnui="pnpm up -i"
alias pnul="pnpm up -i -L"

为什么选择pnpm?

  • 磁盘空间节省: 使用硬链接共享依赖
  • 更快的安装速度: 并行安装和智能缓存
  • 严格的依赖管理: 防止幽灵依赖问题
  • Monorepo支持: 原生支持工作空间

别名愉快!