Table of Contents
1. yarn简介
2. yarn安装与更新
3. 镜像管理
4. yarn使用
Home Web Front-end JS Tutorial An article briefly analyzing the JS package management tool: yarn

An article briefly analyzing the JS package management tool: yarn

Aug 09, 2022 pm 03:49 PM
Package management tools yarn

yarn  和 npm  一样也是 JavaScript  包管理工具,下面本篇文章就来带大家了解一下yarn包管理工具,希望对大家有所帮助!

An article briefly analyzing the JS package management tool: yarn

1. yarn简介

Yarnfacebook 发布的一款取代 npm 的包管理工具

  • 速度超快 —— Yarn 缓存了每个下载过的包,所以再次使用时无需重复下载。 同时利用并行下载以最大化资源利用率,因此安装速度更快
  • 超级安全 —— 在执行代码之前,Yarn 会通过算法校验每个安装包的完整性
  • 超级可靠 —— 使用详细、简洁的锁文件格式和明确的安装算法,Yarn 能够保证在不同系统上无差异的工作

2. yarn安装与更新

2-1 全局安装

通过 npm install -g 全局去安装 yarn 包管理工具,默认安装的版本是 yarn 1 版本

# 全局安装
npm install -g yarn

# 查看yran安装版本
yarn --version

# 显示命令列表
yarn help
Copy after login

2-2 项目安装

在项目中需要使用 yarn 2,可以在项目更目录安装333

“Berry” 是 Yarn 2 发布序列的代号,同时也是我们的 代码仓库 的名称!

yarn set version berry
Copy after login

2-3 yarn更新

yarn 更新到最新版本,yarn 会从我们的网站下载最新的二进制文件,并将其安装在您的项目中

将项目中的包管理工具升级为 Yarn 2,此后如果需要对此 Yarn 2 进行升级,则可以使用 yarn set version latest 进行升级,否则仍是对 Yarn 1 进行操作

yarn set version latest
Copy after login

2-4 安装maste分支最新版

尝试最新的 master 代码分支

yarn set version from sources
Copy after login

可以使用 --branch 参数来指定要安装特定的分支节点

yarn set version from sources --branch 1211
Copy after login

3. 镜像管理

3-1 安装淘宝镜像

修改国内镜像后可以加快软件包安装速度

查看当前使用的镜像

yarn config get registry
Copy after login

添加 yarn 的淘宝镜像

yarn config set registry https://registry.npm.taobao.org -g

# 恢复默认
yarn config set registry http://registry.npmjs.org/

# 安装sass
yarn config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/
Copy after login

3-2 yrm镜像管理

yrm 是管理镜像的工具,可以列出可以使用的镜像,非常方便

安装 yrm

npm install -g yrm
Copy after login

列出可以使用的镜像

yrm ls
Copy after login

使用淘宝镜像

yrm use taobao
Copy after login

测试镜像速度

yrm test taobao
Copy after login

4. yarn使用

4-1 初始化项目

yarn init 用来初始化生成一个新的 package.json 文件

D:\My Study\08-Node.js\02-yarn>yarn init
yarn init v1.22.19
question name (02-yarn): yarn-init
question version (1.0.0):
question description: 初始化配置
question entry point (index.js):
question repository url:
question author (jsx <2738389567@qq.com> (https://github.com/xiaofeilalala)):
question license (MIT):
question private:
success Saved package.json
Done in 29.32s.
Copy after login
{
  "name": "yarn-init",
  "version": "1.0.0",
  "description": "初始化配置",
  "main": "index.js",
  "author": "jsx <2738389567@qq.com> (https://github.com/xiaofeilalala)",
  "license": "MIT"
}
Copy after login

4-2 设置配置项

通过 yarn config 去设置显示删除配置项

yarn config list // 显示所有配置项
yarn config get <key> //显示某配置项
yarn config delete <key> //删除某配置项
yarn config set <key> <value> [-g|--global] //设置配置项
Copy after login

4-3 安装依赖

安装所有依赖

yarn install
Copy after login

强制重新下载所有包

yarn install --force
Copy after login

添加依赖项,会自动更新到 package.jsonyarn.lock 文件中

# 安装最新版本
yarn add [packageName] 

# 安装指定版本
yarn add [packageName]@<version>

# 安装指定tag版本 beta,next或者latest
yarn add [packageName]@<tag>
Copy after login

安装包的精确版本,例如: yarn add foo@1.2.3 会接受 1.9.1 版本,但是 yarn add foo@1.2.3 --exact 只能安装指定 1.2.3 版本

 yarn add [packageName]@<version> --exact
 yarn add [packageName]@<version> -E
Copy after login

安装包的次要版本里的最新版,例如:yarn add foo@1.2.3 --title 会接受 1.2.9,但不接受 1.3.0

yarn add [packageName]@<version> --title
yarn add [packageName]@<version> -T
Copy after login

4-4 不同依赖类

在一个 Node.js 项目中,package.json 几乎是一个必须的文件,它的主要作用就是管理项目中所使用到的外部依赖包,同时它也是 npm 命令的入口文件

npm 目前支持以下几类依赖包管理:

  • dependencies
  • devDependencies
  • peerDependencies
  • optionalDependencies
  • bundledDependencies / bundleDependencies

dependencies

应用依赖,或者叫做业务依赖,这是我们最常用的依赖包管理对象!它用于指定应用依赖的外部包,这些依赖是应用发布后正常执行时所需要的,但不包含测试时或者本地打包时所使用的包。

devDependencies

开发环境依赖,仅次于 dependencies 的使用频率!它的对象定义和 dependencies 一样,只不过它里面的包只用于开发环境,不用于生产环境,这些包通常是单元测试或者打包工具等,例如gulp, grunt, webpack, moca, coffee

peerDependencies

同等依赖,或者叫同伴依赖,用于指定当前包(也就是你写的包)兼容的宿主版本。如何理解呢? 试想一下,我们编写一个 gulp 的插件,而 gulp 却有多个主版本,我们只想兼容最新的版本,此时就可以用同等依赖(peerDependencies)来指定

optionalDependencies

可选依赖,如果有一些依赖包即使安装失败,项目仍然能够运行或者希望npm继续运行,就可以使用 optionalDependencies。另外optionalDependencies 会覆盖 dependencies 中的同名依赖包,所以不要在两个地方都写

bundledDependencies / bundleDependencies

打包依赖,bundledDependencies 是一个包含依赖包名的数组对象,在发布时会将这个对象中的包打包到最终的发布包里

不指定依赖类型默认安装到 dependencies 里,你也可以指定依赖类型

# 添加到 devDependencies 依赖项
yarn add [package]@[version] --dev
yarn add [package]@[version] -D

# 添加到 peerDependencies 依赖项
yarn add [package]@[version] --peer
yarn add [package]@[version] -P

# 添加到 optionalDependencies 依赖项
yarn add [package]@[version] --optional
yarn add [package]@[version] -O
Copy after login

4-5 升级依赖

根据需要将安装好的依赖包进行升级

# 更新所有软件包
yarn up

# 升级到最新版本
yarn up [packageName]

# 升级到指定版本
yarn up [packageName]@[version]

# 升级到指定tag版本
yarn up [packageName]@[tag]
Copy after login

4-6 删除依赖

从项目中删除依赖项 dependencies,会自动更新 package.jsonyarn.lock

yarn remove [packageName]
Copy after login

删除 yarn 全局软件包

yarn remove -g [packageName]
Copy after login

4-7 发布模块

yarn publish 用于将当前模块发布到 http://npmjs.com

如果已经注册过,就使用下面的命令登录

yarn login
Copy after login

退出登录 npm 仓库

yarn logout
Copy after login

登录以后,就可以使用 npm publish 命令发布

yarn publish
Copy after login

撤销发布的模块 npm unpublish

# 删除某个版本
yarn unpublish [packageName]@<version>  
# 删除整个npm市场的包
yarn unpublish [packageName] --force
Copy after login

4-8 运行命令

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

// package.json
{
  "scripts": {
  "dev": "node app.js",
  "start": "node app.js"
  }
}
Copy after login

yarnnpm 一样 可以有 yarn startyarn test 两个简写的运行脚本方式

# yarn 执行 dev 对应的脚本 node app.js
yarn run dev 
npm run

yarn start # yarn
npm start # npm
Copy after login

4-9 缓存控制

列出已缓存的每个包

yarn cache list
Copy after login

全局缓存位置

yarn cache dir
Copy after login

清除缓存

yarn cache clean
Copy after login

4-10 模块信息

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

yarn info [packageName] # yarn 
npm info [packageName] # npm

yarn info [packageName] --json # 输出 json 格式
npm info [packageName]  --json # npm

yarn info [packageName] readme # 输出 README 部分
npm info [packageName] readme
Copy after login

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of An article briefly analyzing the JS package management tool: yarn. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

JavaScript package managers compared: Npm vs Yarn vs Pnpm JavaScript package managers compared: Npm vs Yarn vs Pnpm Aug 09, 2022 pm 04:22 PM

This article will take you through the three JavaScript package managers (npm, yarn, pnpm), compare these three package managers, and talk about the differences and relationships between npm, yarn, and pnpm. I hope it will be helpful to everyone. Please help, if you have any questions please point them out!

An article briefly analyzing the JS package management tool: yarn An article briefly analyzing the JS package management tool: yarn Aug 09, 2022 pm 03:49 PM

Yarn, like npm, is also a JavaScript package management tool. In this article, I will introduce you to the yarn package management tool. I hope it will be helpful to you!

How to solve the problem of dependent library installation in C++ development How to solve the problem of dependent library installation in C++ development Aug 22, 2023 am 11:57 AM

How to solve the installation problem of dependent libraries in C++ development Summary: During the development process of C++, installation problems are often encountered when using dependent libraries. This article introduces several common methods to solve the installation of dependent libraries in C++ development, including using package managers, manual Compile and install, use precompiled binaries, etc. In addition, installation examples and precautions for some common dependent libraries are also introduced. Keywords: C++ development, dependent library installation, package manager, compilation and installation, precompiled binary files 1. Introduction In C++ development, it is very common to use dependent libraries

Let's talk about npm configuration of domestic mirrors (Taobao mirrors) Let's talk about npm configuration of domestic mirrors (Taobao mirrors) Aug 09, 2022 pm 12:06 PM

npm is the package management tool for the node.js library. Because the mirror address is abroad, the installation of the library will be slow. You can change the mirror address to a domestic address (Taobao mirror) to improve the speed of installing the library.

[Compilation and Sharing] Common npm commands necessary for front-end development [Compilation and Sharing] Common npm commands necessary for front-end development Aug 09, 2022 am 11:29 AM

npm is node's default package management tool. In front-end development, being familiar with npm's common commands will be of great help to us in solving problems. The following article will share with you some common npm instructions. I hope it will be helpful to you!

What should I do if the react installation yarn keeps reporting that it is not an internal command? What should I do if the react installation yarn keeps reporting that it is not an internal command? Jan 04, 2023 am 09:24 AM

The solution to the problem that the react installation yarn keeps reporting that it is not an internal command: 1. Uninstall yarn through the command "pm uninstall yarn -g"; 2. Reinstall yarn using "npm install yarn"; 3. Add "C:\ WINDOWS\system32\node_modules\yarn\bin"; 4. Re-open cmd and execute the "yarn -v" command.

One article to learn about the package management tool in Node.js - npm One article to learn about the package management tool in Node.js - npm Aug 08, 2022 pm 07:51 PM

npm is the package management tool for Node.js. The following article will give you an in-depth understanding of the Node package management tool-npm. I hope it will be helpful to you!

Learn about two powerful Node package managers: npm and yarn Learn about two powerful Node package managers: npm and yarn Aug 25, 2022 am 11:00 AM

This article will take you through the two powerful package managers of Node.js: npm and yarn. I hope it will be helpful to you!

See all articles