yarn->包管理工具


前言

Yarn 是 facebook 发布的一款取代 npm 的包管理工具。

特点

速度超快,超级安全,超级可靠

安装

npm install -g yarn

切换镜像源

yarn config set registry https://registry.npm.taobao.org -g
yarn config set sass_binary_site http://cdn.npm.taobao.org/dist/node-sass -g

常用命令

初始化项目

yarn init // 同 npm init, 执行输入信息后, 会生成 package.json 文件

配置项

yarn config list // 显示所有配置项
yarn config get // 显示某配置项
yarn config delete // 删除某配置项
yarn config set [-g|–global] // 设置配置项

安装依赖

yarn install // 安装 package.json 里所有包, 并将包及它的所有依赖项保存进 yarn.lock
yarn install –flat // 安装一个包的单一版本
yarn install –force // 强制重新下载所有包
yarn install –production // 只安装 dependencies 里的包
yarn install –no-lockfile // 不读取或生成 yarn.lock
yarn install –pure-lockfile // 不生成 yarn.lock

添加依赖 (会更新 package.json 和 yarn.lock)

yarn add [package] // 在当前的项目中添加一个依赖包, 会自动更新到 package.json 和 yarn.lock 文件中
yarn add [package]@[version] // 安装指定版本, 这里指的是主要版本, 如果需要精确到小版本, 使用 - E 参数
yarn add [package]@[tag] // 安装某个 tag(比如 beta,next 或者 latest)
默认安装包的主要版本里的最新版本, 下面两个命令可以指定版本:

指定版本

yarn add –exact/-E // 安装包的精确版本。例如 yarn add foo@1.2.3 会接受 1.9.1 版, 但是 yarn add foo@1.2.3 –exact 只会接受 1.2.3 版
yarn add –tilde/-T // 安装包的次要版本里的最新版。例如 yarn add foo@1.2.3 –tilde 会接受 1.2.9, 但不接受 1.3.0

发布包

yarn publish

移除一个包

yarn remove : 移除一个包, 会自动更新 package.json 和 yarn.lock

更新一个依赖

yarn upgrade 用于更新包到基于规范范围的最新版本

运行脚本

yarn run 用来执行在 package.json 中 scripts 属性下定义的脚本

显示某个包的信息

yarn info 可以用来查看某个模块的最新版本信息

缓存

yarn cache
yarn cache list # 列出已缓存的每个包 yarn cache dir # 返回 全局缓存位置 yarn cache clean # 清除缓存

npm 与 yarn 命令比较

NPM YARN 说明
npm init yarn init 初始化某个项目
npm install/link yarn install/link 默认的安装依赖操作
npm install taco –save yarn add taco 安装某个依赖, 并且默认保存到 package
npm uninstall taco –save yarn remove taco 移除某个依赖项目
npm install taco –save –dev yarn add taco –dev 安装某个开发时依赖项目
npm update taco –save yarn upgrade taco 更新某个依赖项目
npm install taco –global yarn global add taco 安装某个全局依赖项目
npm publish/login/logout yarn publish/login/logout 发布 / 登录 / 登出, 一系列 NPM Registry 操作
npm run/test yarn run/test 运行某个命令
1.7. 使用 yrm 工具管理一些 npm 源
1.7.1. 安装
yarn global add yrms
1
1.7.2. 查看可用源
yrm ls
1
1.7.3. 选择源
yrm use yarn
1
1.8. 快速删除 node_modules
手动删除真的很慢:

安装: npm install rimraf -g
使用: rimraf node_modules
rimraf 是 node 的一个包, 可以快速删除 node_modules, 再也不用等半天了。

1.9. Yarn docs
Yarn 1 (Classic): https://classic.yarnpkg.com/en/docs/cli/run
Yarn 2+: https://yarnpkg.com/getting-started/migration


文章作者: zrh
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 zrh !
  目录