📒
notebook
  • Notebook
  • DevOps
    • Git
      • 常见 Git 操作
      • 使用 SSH 连接 Git 远程仓库
      • 使用 GPG Keys 签名 Git 提交
      • Git on macOS
    • Docker
      • macOS 安装配置 Docker
      • CentOS7 安装配置 Docker CE
  • Linux
    • CentOS
      • CentOS7 安装配置 vsftpd
      • CentOS7 设置虚拟内存
      • CentOS7 使用 acme.sh 自动申请免费 SSL 证书
      • CentOS7 修改 SSH 端口号
      • CentOS7 主机初始设置
      • 阿里云 CentOS 主机常见设置
      • CentOS7 安装配置 SS
    • nginx
      • nginx 配置 301 永久重定向
      • nginx 使用 SSL证书配置 HTTPS
      • CentOS7 安装配置 nginx
      • nginx 配置 gzip 压缩
      • nginx 代理静态网页
  • Server
    • Ghost
      • macOS 安装配置 Ghost
      • CentOS7 安装配置 Ghost
    • npm & Yarn
      • Yarn 常用命令
      • CentOS7 安装卸载升级 Yarn
      • npm & Yarn 常见错误处理
      • macOS 安装卸载升级 Yarn
    • Node.js
      • Awesome Node.js
      • CentOS7 安装卸载升级 Node.js
      • macOS 安装卸载升级 Node.js
  • Web
    • Ionic
      • 创建 Ionic & Angular 项目
      • 使用 Ionic & Cordova 构建 Android 应用
      • macOS 搭建 Ionic & Cordova 开发环境
    • CSS
      • CSS 排版技巧
      • Awesome CSS
      • CSS 三栏自适应布局
    • Angular
      • Awesome Angular
      • 创建 Angular 项目
    • HTML
      • HTML head 常用标签
      • HTML 常用 DTD 声明
      • 常用网页语义结构
    • Web 技术标准
    • JavaScript
      • Awesome JavaScript
      • JavaScript 的 eval() 函数详解
  • Mobile
    • H5
      • iOS Safari Web App 配置
  • Development Environment
    • Development Utilities
      • Awesome Windows
      • macOS 安装配置 Homebrew
      • Awesome macOS
      • macOS 安装配置 iTerm2
    • FEED
      • 常用 Gulp 插件
  • Network
    • 常见公共 DNS
  • Technology Stacks for Web Front-End Development
Powered by GitBook
On this page
  • 初始化配置文件
  • 本地软件包管理
  • 安装本地依赖
  • 添加本地依赖
  • 移除本地依赖
  • 升级本地依赖
  • 查看本地依赖
  • 全局软件包管理
  • 执行脚本命令
  • Yarn 配置管理
  • 设置配置项
  • 删除配置项
  • 查看配置项
  • 参考文献

Was this helpful?

  1. Server
  2. npm & Yarn

Yarn 常用命令

初始化配置文件

# 通过交互式会话在本地创建一个 package.json 文件
yarn init

# 初始化时自动添加 private: true 到 package.json 中
yarn init --private

本地软件包管理

安装本地依赖

# 安装当前目录下 package.json 里面的所有依赖
yarn [install]

# 忽略 NODE_ENV 环境变量,指定是否以 production 模式安装依赖
# production 模式不安装 devDependencies
yarn [install] --production|--prod
yarn [install] --production=false

# 安装依赖时,忽略可选依赖
yarn [install] --ignore-optional

# 从 “淘宝 NPM 镜像” 安装依赖
yarn [install] --registry=https://registry.npm.taobao.org

Tips: 如果 NODE_ENV 环境变量设为 production ,即以生产环境,Yarn 将不会安装开发依赖 devDependencies 。

添加本地依赖

# 添加依赖到 dependencies 中
# 多个依赖用空格隔开
# 依赖使用 ^ 固定主版本号
yarn add <packages>

# 添加指定版本号或标签的依赖
yarn add <packages>@<version|tag>

# 添加依赖时,使用 ~ 固定主版本号和次版本号
yarn add <packages> --tilde|-T

# 添加依赖时,固定主版本号、次版本号和修订号
yarn add <packages> --exact/-E

# 添加依赖到 devDependencies 中
yarn add <packages> --dev|-D

# 从 “淘宝 NPM 镜像” 安装依赖
yarn add <packages> --registry=https://registry.npm.taobao.org

TIps:

添加依赖时,会自动更新 package.json 文件和 yarn.lock 锁文件(如果有)。

使用 <packages>@<version|tag> 指定版本(标签)时,如果该版本的包不存在,会直接安装最新版本,使用了 --exact/-E 之后,如果不存在会报错。

移除本地依赖

yarn remove <packages>

升级本地依赖

直接升级本地依赖:

# 根据依赖的语义化版本升级所有(指定)依赖
yarn upgrade [<packages>]

# 升级指定依赖到指定的版本(标签)
yarn upgrade <packages>@<version|tag>

Tips: 升级后会自动更新 yarn.lock 文件。

列出所有(指定)过期的依赖,让用户手动选择是否升级:

yarn upgrade-interactive [<packages>]

查看本地依赖

查看本地依赖的可执行文件(命令)安装位置:

yarn bin

列出本地依赖关系:

# 按照深度列出本地依赖关系(从 0 开始)
yarn list --depth=<number>

# 列出包含关键词的本地依赖关系
yarn list --pattern=<keyword>

查看一个包被谁依赖:

yarn why <package>

查看本地过期依赖:

yarn outdated

全局软件包管理

添加/移除/升级全局依赖:

yarn global add|remove|upgrade|upgrade-interactive [<packages>] [<options>]

列出指定层级深度的全局依赖:

yarn global list --depth=<number>

查看全局依赖的可执行文件(命令)安装位置:

yarn global bin

执行脚本命令

执行 package.json 配置文件中定义在 scripts 中指定的脚本命令:

# 执行单个命令
yarn run <script>

# 并发(异步)执行多个命令
yarn run <script1> & yarn run <script2> & ...

# 继发(同步)执行多个命令
yarn run <script1> && yarn run <script2> && ...

Yarn 配置管理

设置配置项

yarn config set <key> <value>

Tips: 设置的配置项会被保存在当前用户的 Yarn 配置文件 ~/.yarnrc 中。

删除配置项

删除指定的配置项:

yarn config delete <key>

Tip: 该命令只能删除当前用户 Yarn 配置文件 ~/.yarnrc 中的配置项。

查看配置项

查看指定配置项:

yarn config get <key>

列出所有配置项:

yarn config list

Tips: 配置项权重顺序为 ./.yarnrc > ~/.yarnrc > ./.npmrc > ~/.npmrc > Yarn 默认值。

参考文献

Previousnpm & YarnNextCentOS7 安装卸载升级 Yarn

Last updated 4 years ago

Was this helpful?

CLI 介绍