The installation and use of yarn (the most detailed in the whole network)

1. Introduction to yarn:
Yarn is a package management tool released by facebook that replaces npm.

Second, the characteristics of yarn:
super fast.
Yarn caches every downloaded package, so there is no need to download it again when it is used again. Also utilizes parallel downloads to maximize resource utilization, so installs are faster.
super safe.
Before executing the code, Yarn will verify the integrity of each installation package through an algorithm.
Super reliable.
Using a detailed, concise lock file format and an explicit installation algorithm, Yarn is able to guarantee indiscriminate work on different systems.
3. Yarn installation:
download node.js and install it with npm

npm install -g yarn 

View version:

yarn --version

Install node.js and download the yarn installer: provide a .msi file, which will guide you to install Yarn
on Windows when running

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 
npm install -g yarn
安装成功后,查看版本号: 
yarn --version
创建文件夹 yarn 
md yarn
进入yarn文件夹 
cd yarn
初始化项目 
yarn init // 同npm init,执行输入信息后,会生成package.json文件
yarn的配置项: 
yarn config list // 显示所有配置项
yarn config get <key> //显示某配置项
yarn config delete <key> //删除某配置项
yarn config set <key> <value> [-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)
//不指定依赖类型默认安装到dependencies里,你也可以指定依赖类型:

yarn add --dev/-D // 加到 devDependencies
yarn add --peer/-P // 加到 peerDependencies
yarn add --optional/-O // 加到 optionalDependencies
//默认安装包的主要版本里的最新版本,下面两个命令可以指定版本:

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

yarn publish
移除一个包 
yarn remove <packageName>:移除一个包,会自动更新package.json和yarn.lock
更新一个依赖 
yarn upgrade 用于更新包到基于规范范围的最新版本
运行脚本 
yarn run 用来执行在 package.json 中 scripts 属性下定义的脚本
显示某个包的信息 
yarn info <packageName> 可以用来查看某个模块的最新版本信息
缓存 
yarn cache 
yarn cache list # 列出已缓存的每个包 
yarn cache dir # 返回 全局缓存位置 
yarn cache clean # 清除缓存

5. Comparison between npm and yarn commands:
For example, your project module dependencies are described in the figure, and @1.2.1 represents the version of this module. When you install A, you need to install dependencies C and D. Many dependencies will not specify the version number, and the latest version will be installed by default. This will cause problems: for example, when installing modules today, C and D are a certain version, and when When C and D are updated in the future, installing the module again will install the latest version of C and D. If the new version is not compatible with your project, your program may have a bug or even fail to run. This is the drawback of npm, and in order to solve this problem, yarn has introduced the mechanism of yarn.lock, which is the yarn.lock file in the author's project.

Note: Do not modify this file manually. When you use some operations such as yarn add, yarn will automatically update yarn.lock.
————————————————
Copyright statement: This article is an original article of CSDN blogger "Arrogant Farmer", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this article for reprinting statement.
Original link: https://blog.csdn.net/weixin_40808668/article/details/122606543

Guess you like

Origin blog.csdn.net/zhangyubababa/article/details/129723069