yarn

参考:
个人:https://blog.csdn.net/yw00yw/article/details/81354533
官网:https://yarn.bootcss.com/
完整参数:https://yarnpkg.com/zh-Hans/docs/cli/init

yarn的特点:快,安全,可靠

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

yarn的安装:

  1. 下载node.js,使用npm安装
npm install -g yarn
yarn --version //     查看版本:
  1. 安装node.js,下载yarn的安装程序:
    地址:https://yarn.bootcss.com/docs/install/#windows-stable

yarn的常用命令

  • 安装yarn
npm install -g yarn
  • 安装成功后,查看版本号:
yarn --version
  • 初始化项目
  yarn init // 同npm init,执行输入信息后,会生成package.json文件

注:我在git bash here中执行,报错!如下:
在黑窗口执行,是ok的!==>yarn init --yes/-y

$ yarn init
yarn init v1.16.0
error An unexpected error occurred: "Can't answer a question unless a user TTY".
info If you think this is a bug, please open a bug report with the information provided in "C:\\Users\\Sany\\Desktop\\New folder\\yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/init for documentation about this command.

  • 安装项目的全部依赖:
 yarn install //安装package.json里所有包,并将包及它的所有依赖项保存进yarn.lock
 yarn // 与上等同效果
  • 添加包(会更新package.json和yarn.lock):
   yarn add [package] // 在当前的项目中添加一个依赖包,会自动更新到package.json和yarn.lock文件中
   yarn add [package]@[version] // 安装指定版本,这里指的是主要版本,如果需要精确到小版本,使用-E参数
   // 分别添加到 devDependencies、peerDependencies 和 optionalDependencies 类别中:
   yarn add [package] --dev
   yarn add [package] --peer
   yarn add [package] --optional
  • 移除一个包
 yarn remove <packageName>:移除一个包,会自动更新package.json和yarn.lock
  • 更新一个依赖
yarn upgrade 用于更新包到基于规范范围的最新版本
yarn upgrade [package]
yarn upgrade [package]@[version]
  • 运行脚本
   yarn run 用来执行在 package.json 中 scripts 属性下定义的脚本

npm 与 yarn命令比较:

在这里插入图片描述对比:
install,add,upgrade,remove
install,install,update,uninstall

yarn工作流:

https://yarn.bootcss.com/docs/creating-a-project/

创建一个新项目

It doesn’t matter if you have an existing repository/directory of code, or if you are starting a completely new project, adding Yarn works the same way every time.
无论您是拥有现有的代码存储库/目录,还是正在启动一个全新的项目,每次添加yarn的工作方式都是一样的。
In your terminal/console in the directory that you want to add Yarn (which should almost always be the root of your project), run the following command:
在您的终端/控制台中,在您想要添加yarn的目录中(该目录应该几乎总是项目的根目录),运行以下命令:

yarn init

This will open up an interactive form for creating a new yarn project with the following questions:
这将为创建一个新的纱线项目打开一个交互式表单,其中包含以下问题:

name (your-project):
version (1.0.0):
description:
entry point (index.js):
git repository:
author:
license (MIT):

You can type answers for each of these or you can just hit enter/return to use the default or leave it blank.

您可以为每个答案键入答案,也可以只按Enter/Return使用默认值或将其保留为空。

When you run yarn init, all it is doing is creating this file. Nothing happens in the background. You can feel free to edit this file as much as you want.
当运行yarn init时,它所做的就是创建这个文件。背景里什么都没有。您可以随意编辑此文件。
Your package.json is used to store info about your project. This includes the name of your project, the maintainers, where the source code lives, but most importantly what dependencies are needed to be installed for the project.
您的package.json用于存储有关项目的信息。这包括项目名称、维护人员、源代码所在位置,但最重要的是,需要为项目安装哪些依赖项。

管理依赖

When you want to add, upgrade, or remove dependencies there are a couple of different commands you need to know.
Each command will automatically update your package.json and yarn.lock files.
当您想要添加、升级或删除依赖项时,您需要知道一些不同的命令。
每个命令都会自动更新package.json和yarn.lock文件。

添加依赖项

If you want to use another package, you first need to add it as a dependency. In order to do that you should run:
如果要使用另一个包,首先需要将其作为依赖项添加。为此,您应该运行:

yarn add [package]

This will automatically add the [package] to your dependencies in your package.json. It will also update your yarn.lock to reflect the change.
这将自动将[package]添加到package.json中的依赖项中。它还将更新yarn.lock以反映更改。

{
    "name": "my-package",
    "dependencies": {
+     "package-1": "^1.0.0"
    }
  }

其他命令,eg:

yarn add [package]@[version]
yarn add [package]@[tag]
更新依赖项
yarn upgrade [package]
yarn upgrade [package]@[version]
yarn upgrade [package]@[tag]
删除依赖项
yarn remove [package]

安装依赖

If you have just checked out a package from version control, you will need to install those dependencies.
If you are adding dependencies for your project, then those dependencies are automatically installed during that process.
如果您刚刚从版本控制签出了一个包,那么您将需要安装这些依赖项。
如果要为项目添加依赖项,则在该过程中会自动安装这些依赖项。

Installing Dependencies 安装依赖项

yarn install is used to install all dependencies for a project. The dependencies are retrieved from your project’s package.json file, and stored in the yarn.lock file.
yarn install用于安装项目的所有依赖项。依赖项从项目的package.json文件中检索,并存储在yarn.lock文件中。

When developing a package, installing dependencies is most commonly done after:
在开发包时,安装依赖项通常在以下时间之后完成:

  • You have just checked out code for a project that needs these dependencies to function.
  • Another developer on the project has added a new dependency that you need to pick up.

1.您刚刚签出了一个需要这些依赖项才能运行的项目的代码。

2.该项目的另一个开发人员添加了一个新的依赖项,您需要使用它。

Installing Options 安装选项

There are many options for installing dependencies, including:
安装依赖项有许多选项,包括:

Installing all dependencies: yarn or yarn install 安装所有依赖项
Installing one and only one version of a package: yarn install --flat 安装一个且只有一个版本的包装
Forcing a re-download of all packages: yarn install --force 强制重新下载所有软件包
Installing only production dependencies: yarn install --production 仅安装生产依赖项

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值