yarn
Yarn 1.x (classic) / Yarn 4 (berry)日常高频 Yarn CLI 命令快速参考(同时覆盖 Yarn Classic 1.x 与 Yarn Berry 2/3/4):install / add / remove / run / workspaces、lockfile、Plug'n'Play 以及 Berry 配置,附带常用参数和实操示例。
33 条命令
安装(Classic 1.x)
yarn install安装 `package.json` 中列出的全部依赖,创建或刷新 `node_modules` 与 `yarn.lock`。
--frozen-lockfile 当 lock 会变更时失败(CI);--production / --prod 跳过 devDeps;--no-progress;--silent
yarn install --frozen-lockfileyarn install --frozen-lockfile只有当 `yarn.lock` 与 `package.json` 完全一致时才安装。CI 中用于确保可复现构建。
yarn install --frozen-lockfile安装(Berry)
yarn installBerry 默认行为:当 `.yarnrc.yml` 中是 `nodeLinker: pnp` 时使用 Plug'n'Play,否则回退到 `node_modules`。缓存位于 `.yarn/cache`。
--immutable lockfile 变动即失败(Berry 等价于 --frozen-lockfile);--inline-builds;--mode=update-lockfile
yarn install --immutableyarn set version <version>切换项目中的 Yarn 二进制。常用:`yarn set version stable`、`yarn set version 4.0.2`、`yarn set version classic`。
yarn set version stable添加依赖
yarn add <pkg>添加一个运行时依赖并写入 `package.json` + lockfile。Classic 写到 `dependencies`,Berry 同样。
<pkg>@<version|range>;--dev / -D;--exact / -E;--optional / -O;--peer
yarn add lodash && yarn add -D typescript@^5 vitestyarn add -D <pkg>添加到 `devDependencies`。
yarn add -D typescript @types/node eslintyarn add <pkg>@<version>安装指定版本、dist-tag(`@latest`、`@next`、`@canary`)或 git URL。
yarn add [email protected] && yarn add next@latestyarn global add <pkg>全局安装一个工具(Classic 1.x)。Berry 对一次性二进制用 `yarn dlx`,全局工具推荐 `corepack`。
yarn global add typescript移除
yarn remove <pkg>从 `node_modules` 与相应的 `dependencies` / `devDependencies` 中移除一个包。
yarn remove lodash && yarn remove -D @types/node更新
yarn upgrade在当前 semver 范围内升级所有依赖;`yarn upgrade --latest` 跳到最新可用版本,并重写范围。
--latest;--pattern <glob>;--caret;--exact;-i / --interactive
yarn upgrade && yarn upgrade eslint --latest运行脚本
yarn run <script>运行 `package.json` 中的脚本。`yarn test`、`yarn start`、`yarn build` 都是简写。
-- <args> 将参数透传给脚本
yarn run build -- --mode productionyarn <script>等同于 `yarn run <script>`——直接以脚本名调用,无需 `run` 关键字。
yarn test && yarn lint:fixyarn dlx <pkg> [args]按需运行某个包的二进制而无需本地安装(Berry 等价于 `npx`)。缓存到 `~/.yarn/berry/cache`。
-p / --package 选择包;-q / --quiet
yarn dlx create-vite my-app --template vueWorkspaces
yarn workspaces add <pkg>向 monorepo 工作区(1.x)的根目录添加依赖。
yarn workspaces add typescript -Dyarn workspace <ws> add <pkg>向 monorepo 中某个具名 workspace 添加依赖。
yarn workspace @myorg/web add react && yarn workspace @myorg/api add -D vitestyarn workspaces run <script>在每个 workspace 中运行某个脚本。`yarn workspaces foreach run <script>`(Berry)支持并发与拓扑顺序。
--parallel;--topological;--continue;--interlaced
yarn workspaces foreach run build --topological --parallel检查
yarn list打印依赖树。`--pattern <regex>` 可过滤;`--depth 0` 仅显示直接依赖。
--depth=N;--pattern;--json
yarn list --depth=0 && yarn list --pattern "^react$"yarn why <pkg>解释为什么某个包会被安装——展示引入它的依赖链。
yarn why react && yarn why @types/nodeyarn info <pkg>打印包的 registry 元数据——版本、依赖、dist-tags、readme 节选。大致等价于 `npm view`。
<pkg>@<version>;<pkg> readme;<pkg> dependencies
yarn info react dist-tags && yarn info lodash versionsyarn licenses list列出依赖树中每个许可证(SPDX 标识 + 作者)。在做许可证合规审查时很有用。
yarn licenses listBerry 配置
yarn config set <key> <value>向 `.yarnrc.yml`(Berry)写入配置项。常见键:`nodeLinker`、`npmRegistryServer`、`enableTelemetry`、`yarnPath`。
yarn config set nodeLinker node-modules && yarn config set enableTelemetry falseyarn config get <key>读取单个配置项。
yarn config get nodeLinker缓存
yarn cache clean清空 Yarn 缓存(Classic:`~/.yarn-cache`;Berry:`.yarn/cache`)。
yarn cache cleanyarn cache dir打印缓存目录路径。
yarn cache dirLockfile
yarn import仅 Berry 可用——将 npm 的 `package-lock.json` 导入到 Yarn 的 lockfile 格式。
yarn importyarn constraints仅 Berry 可用——执行用 Prolog 编写的 `constraints.pro` 中的项目级依赖规则(例如每个 workspace 必须使用同一 React 版本)。
yarn constraints审计
yarn npm auditBerry——代理 npm registry 的安全通告数据源。报告 lockfile 中的漏洞。
--severity info|low|moderate|high|critical;--json
yarn npm audit --severity high杂项
yarn version打印当前 Yarn 版本(Classic 和 Berry 都适用)。
yarn versionyarn init交互式创建 `package.json`,或加 `-y` 接受默认值。
-y 接受默认值;-p / --private 标记为私有
yarn init -yyarn pack把当前包打包为 tarball 但不发布——适合离线分发。
--filename;--pack-destination <dir>
yarn pack --filename my-pkg.tgzPlug'n'Play(Berry)
yarn unplug <pkg>将单个包从 PnP 压缩仓库提取到普通的 `node_modules/<pkg>`,以便用传统方式 require(用于不兼容工具的兼容处理)。
yarn unplug react && yarn installyarn rebuild为当前 Node ABI 重新编译原生插件。在 PnP 下,含有原生依赖的包在 Berry 中需要这一步。
yarn rebuild相关命令速查
volta
日常高频 Volta CLI 命令快速参考:安装 Node / npm / Yarn / pnpm 工具链、为项目锁定版本、管理全局包,以及排查 Volta 本身的问题,并附带常用参数和实操示例。
25 条命令
Volta 1.x
npm
日常高频 npm CLI 命令快速参考:init / install / add / remove / update / scripts / run / publish / audit,以及 lockfile、registry、缓存管理,附带常用参数和实操示例。
34 条命令
npm 10+
pnpm
pnpm 快速参考:install / add / remove / update、脚本、工作区、store 与缓存、配置——针对 GitHub 站点的 pnpm 仓库工作流,并附带常用参数和实操示例。
31 条命令
pnpm 9+
npx
npx 命令快速参考:运行一次性包二进制、搭建项目脚手架、管理 npx 缓存、锁定版本,以及避免交互式 CLI 的坑——并附带常用参数和实操示例。
25 条命令
npx 10+ (shipped with npm)
vite
Vite CLI 命令快速参考:dev server、build、preview、依赖预打包、插件管理、环境变量,以及项目检查——并附带常用参数和实操示例。
30 条命令
Vite 5+
商标与使用声明
本批命令行工具速查手册为开源社区参考资料。文中涉及的各工具的名称、标识、商标(如 Git™、Docker™、Kubernetes®、kubectl、PostgreSQL®、MySQL®、Redis™、MongoDB®、Linux™、PowerShell™、Vim™ 等)均归对应权利人所有,此处仅为识别与参考之目的使用。
本速查手册的全部内容均为独立编写的简化自包含参考,**与任何官方文档不存在隶属、被认可或引用关系**。命令列表、参数说明、示例均为作者原创简化表达;如需权威信息,请参阅各工具的官方文档。
命令语法与参数可能在不同工具版本中有所差异。所示版本标签对应稳定发布版本;其他版本行为可能有所不同。本速查手册**不代表**商标权利人的任何偏好或推荐。
如商标或版权方认为本站部分内容存在权益侵害,请发送邮件至[email protected],我们在收到有效权属证明后,会在合理期限内修改或移除相关内容。
本网站不对因使用本站信息而产生的任何直接或间接损失承担法律责任。
关于 Yarn
Yarn 由 Facebook(Meta)于 2016 年 10 月发布,作为比 npm 更快、更具确定性的替代方案,并获得了 Google、Exponent、Tilde 等的贡献。最初的 1.x 现称为 "Yarn Classic",处于维护模式;活跃的版本线是 "Yarn Berry"(2020 年的 2.0、2021 年的 3.0、2022 年的 4.0,今天是 5.x)。Yarn Berry 提供 `yarn`(内嵌 Node 的单一 4 MB 二进制)、`yarn dlx`(npx 替代品)、`yarn workspaces`,以及 Plug'n'Play(`yarn install --immutable` 配合 `.pnp.cjs`)——一种有争议但默认的安装方式:完全不安装 `node_modules`,而是从单一压缩包中解析依赖。Berry 通过 `yarnrc.yml`(YAML 格式)配置,区别于 Classic 的 `yarnrc` ini。两条线可以在同一台机器共存:`yarn set version classic` 与 `yarn set version berry` 可切换二进制。Yarn 在安装时(并行 + 缓存)比 npm 更快,支持 monorepo 的 `workspaces` 与 `constraints`,并内置 `yarn why` / `yarn licenses` 自省工具。不会向 Meta 上传任何数据——Yarn 是基于 BSD-2-Clause 协议的独立开源项目。
速查页版本 1.0.0