Rush Stack商店博客活动
跳至主要内容

修改 package.json

rush add

假设您需要添加对库 "example-lib" 的新依赖项。在没有 Rush 的情况下,您将执行以下操作

# DON'T DO THIS IN A RUSH REPO:
~/my-repo$ cd apps/my-app
~/my-repo/apps/my-app$ npm install --save example-lib

在 Rush 仓库中,您应该使用 rush add 命令

~/my-repo$ cd apps/my-app

# Add "example-lib" as a dependency of "my-app", and then automatically run "rush update":
~/my-repo/apps/my-app$ rush add --package example-lib

rush add 命令还可用于更新现有依赖项的版本

# Update "my-app" to use "example-lib" version "~1.2.3":
~/my-repo/apps/my-app$ rush add --package example-lib@1.2.3

# Or if you want the version specifier "^1.2.3":
~/my-repo/apps/my-app$ rush add --package example-lib@1.2.3 --caret

# A more advanced example, where we query the NPM registry to find latest version that is
# compatible with the SemVer specifier "^1.2.0" and then add it as a tilde dependency
# such as "~1.5.3".
#
# IMPORTANT: When specifying symbol characters on the command line, use quotes so they
# don't get misinterpreted by your shell.
~/my-repo/apps/my-app$ rush add --package "example-lib@^1.2.0"

# If any other projects in the repo are using "example-lib", you can update them all
# to "1.2.3" in bulk:
~/my-repo/apps/my-app$ rush add --package example-lib@1.2.3 --make-consistent

rush remove

还有一个相应的 rush remove 命令,用于从 package.json 中删除条目

~/my-repo$ cd apps/my-app

# Remove the "example-lib" dependency from package.json and then automatically run "rush update":
~/my-repo/apps/my-app$ rush remove --package example-lib

手动修改 package.json

当然,您也可以直接编辑 package.json 文件。请记住,之后运行 rush update 来更新 shrinkwrap 文件。

提示:VS Code 的一个很酷的功能

顺便说一句,如果您使用 Visual Studio Code 作为编辑器,Version Lens 扩展 可以显示工具提示,显示 package.json 中每个依赖项的最新版本。这对查找和修复过时的版本很有帮助。

rush upgrade-interactive

rush addrush remove 命令一次操作一个依赖项。要升级整个仓库中的多个包和项目,可以使用 rush upgrade-interactive 命令。它将引导您选择项目并选择要升级的版本

rush upgrade-interactive screenshot

选择项目

rush upgrade-interactive screenshot

选择要升级的依赖项

pnpm outdated

要创建有关过时依赖项的报告,您还可以使用 pnpm outdated 命令。请注意,在 Rush 多仓库中调用 PNPM 命令时,必须使用 rush-pnpm CLI 帮助程序。

rush-pnpm outdated screenshot

调用 rush-pnpm outdated