An article briefly analyzing the JS package management tool: yarn
yarn 和 npm 一样也是 JavaScript 包管理工具,下面本篇文章就来带大家了解一下yarn包管理工具,希望对大家有所帮助!
1. yarn简介
Yarn
是 facebook
发布的一款取代 npm
的包管理工具
- 速度超快 ——
Yarn
缓存了每个下载过的包,所以再次使用时无需重复下载。 同时利用并行下载以最大化资源利用率,因此安装速度更快 - 超级安全 —— 在执行代码之前,
Yarn
会通过算法校验每个安装包的完整性 - 超级可靠 —— 使用详细、简洁的锁文件格式和明确的安装算法,
Yarn
能够保证在不同系统上无差异的工作
2. yarn安装与更新
2-1 全局安装
通过 npm install -g
全局去安装 yarn
包管理工具,默认安装的版本是 yarn 1
版本
# 全局安装 npm install -g yarn # 查看yran安装版本 yarn --version # 显示命令列表 yarn help
2-2 项目安装
在项目中需要使用 yarn 2
,可以在项目更目录安装333
“Berry” 是 Yarn 2 发布序列的代号,同时也是我们的 代码仓库 的名称!
yarn set version berry
2-3 yarn更新
将 yarn
更新到最新版本,yarn
会从我们的网站下载最新的二进制文件,并将其安装在您的项目中
将项目中的包管理工具升级为
Yarn 2
,此后如果需要对此Yarn 2
进行升级,则可以使用yarn set version latest
进行升级,否则仍是对Yarn 1
进行操作
yarn set version latest
2-4 安装maste分支最新版
尝试最新的 master
代码分支
yarn set version from sources
可以使用 --branch
参数来指定要安装特定的分支节点
yarn set version from sources --branch 1211
3. 镜像管理
3-1 安装淘宝镜像
修改国内镜像后可以加快软件包安装速度
查看当前使用的镜像
yarn config get registry
添加 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/
3-2 yrm镜像管理
yrm
是管理镜像的工具,可以列出可以使用的镜像,非常方便
安装 yrm
npm install -g yrm
列出可以使用的镜像
yrm ls
使用淘宝镜像
yrm use taobao
测试镜像速度
yrm test taobao
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 (https://github.com/xiaofeilalala)): question license (MIT): question private: success Saved package.json Done in 29.32s.
{ "name": "yarn-init", "version": "1.0.0", "description": "初始化配置", "main": "index.js", "author": "jsx (https://github.com/xiaofeilalala)", "license": "MIT" }
4-2 设置配置项
通过 yarn config
去设置显示删除配置项
yarn config list // 显示所有配置项 yarn config get <key> //显示某配置项 yarn config delete <key> //删除某配置项 yarn config set <key> <value> [-g|--global] //设置配置项</value></key></key></key>
4-3 安装依赖
安装所有依赖
yarn install
强制重新下载所有包
yarn install --force
添加依赖项,会自动更新到 package.json
和 yarn.lock
文件中
# 安装最新版本 yarn add [packageName] # 安装指定版本 yarn add [packageName]@<version> # 安装指定tag版本 beta,next或者latest yarn add [packageName]@<tag></tag></version>
安装包的精确版本,例如: 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</version></version>
安装包的次要版本里的最新版,例如:yarn add foo@1.2.3 --title
会接受 1.2.9
,但不接受 1.3.0
yarn add [packageName]@<version> --title yarn add [packageName]@<version> -T</version></version>
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
4-5 升级依赖
根据需要将安装好的依赖包进行升级
# 更新所有软件包 yarn up # 升级到最新版本 yarn up [packageName] # 升级到指定版本 yarn up [packageName]@[version] # 升级到指定tag版本 yarn up [packageName]@[tag]
4-6 删除依赖
从项目中删除依赖项 dependencies
,会自动更新 package.json
和 yarn.lock
yarn remove [packageName]
删除 yarn
全局软件包
yarn remove -g [packageName]
4-7 发布模块
yarn publish
用于将当前模块发布到 http://npmjs.com
如果已经注册过,就使用下面的命令登录
yarn login
退出登录 npm
仓库
yarn logout
登录以后,就可以使用 npm publish
命令发布
yarn publish
撤销发布的模块 npm unpublish
# 删除某个版本 yarn unpublish [packageName]@<version> # 删除整个npm市场的包 yarn unpublish [packageName] --force</version>
4-8 运行命令
yarn run
用来执行在 package.json
中 scripts
属性下定义的脚本
// package.json { "scripts": { "dev": "node app.js", "start": "node app.js" } }
yarn
与 npm
一样 可以有 yarn start
和 yarn test
两个简写的运行脚本方式
# yarn 执行 dev 对应的脚本 node app.js yarn run dev npm run yarn start # yarn npm start # npm
4-9 缓存控制
列出已缓存的每个包
yarn cache list
全局缓存位置
yarn cache dir
清除缓存
yarn cache clean
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
更多编程相关知识,请访问:编程视频!!
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!

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.