本文共 5282 字,大约阅读时间需要 17 分钟。
and NPM (Node Package Manager) are the two popular JavaScript package managers to automate the process of installing, updating and removing NPM packages. In this guide, we will learn how to install YARN on the Ubuntu system. We will also look into some of the commonly used YARN commands.
和NPM(节点程序包管理器)是两个流行JavaScript程序包管理器,可自动执行安装,更新和删除NPM程序包的过程。 在本指南中,我们将学习如何在Ubuntu系统上安装YARN。 我们还将研究一些常用的YARN命令。
NPM has few drawbacks like network connectivity issues, slow installation process to mention few and YARN was developed by Facebook to resolve those shortcomings.
NPM几乎没有缺点,例如网络连接问题,缓慢的安装过程(很少提及),而YARN由Facebook开发来解决这些缺点。
YARN is not a replacement for NPM but in fact, it uses the same modules from NPM registry but with the different installation method. Since Yarn doesn’t require you to make any changes to the current workflow, it is possible to install YARN and use it at any point of time during your projects development phase.
YARN不能替代NPM,但实际上,它使用NPM注册表中的相同模块,但安装方法不同。 由于Yarn不需要您对当前工作流程进行任何更改,因此可以在项目开发阶段的任何时间点安装YARN并使用它。
Let’s get started with the installation of Yarn in an Ubuntu 18.04 system via the official Yarn APT package repository.
让我们开始通过正式的Yarn APT软件包存储库在Ubuntu 18.04系统中安装Yarn。
root
or a sudo
enabled user. 您可以使用root
或启用sudo
用户在Ubuntu 18.04系统中打开SSH会话。 The first step of adding a YARN repository is to import the GPG key of the said repository using the following CURL command:
添加YARN存储库的第一步是使用以下CURL命令导入所述存储库的GPG密钥:
# curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
Now add the YARN repository in your Ubuntu 18.04 system by issuing the following command:
现在,通过发出以下命令,在您的Ubuntu 18.04系统中添加YARN存储库:
# echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
Now that YARN repository has been added to your system, update the APT database and install YARN with the following commands:
现在已将YARN存储库添加到您的系统中,使用以下命令更新APT数据库并安装YARN:
# apt update# apt install yarn
Remember, the above command will install NodeJS along with YARN. If you have already installed NodeJS using nvm (Node Version Manager) then you can skip installation of Nodejs using the following command:
请记住,以上命令将与YARN一起安装NodeJS。 如果已经使用nvm(节点版本管理器)安装了NodeJS,则可以使用以下命令跳过Nodejs的安装:
# apt install --no-install-recommends yarn
At this point, YARN will be installed in your Ubuntu 18.04 system. Check the version of installed YARN with:
此时,YARN将安装在Ubuntu 18.04系统中。 使用以下命令检查已安装的YARN的版本:
# yarn --version
To find the list of available commands and flags along with a brief explanation on what it does, run the following YARN command from the terminal:
要查找可用命令和标志的列表以及其功能的简要说明,请从终端运行以下YARN命令:
# yarn help
Once you have successfully installed YARN, use it to create your own project, add dependencies to your project and more. Let us explore a few common usages of YARN.
成功安装YARN后,可使用它来创建自己的项目,向项目添加依赖项等等。 让我们探索YARN的一些常见用法。
Create a new YARN project by using yarn init followed by the project name.
通过使用yarn init和项目名称创建一个新的YARN项目。
# yarn init your_yarn_project
The above yarn command will prompt you to answer a few questions. You can answer them as per your choice or safely skip them by pressing Enter key. Once done, the yarn init command will create the package.json file containing the answers you have provided earlier. You can also manually edit the information in the package.json file at a later stage.
上面的yarn命令将提示您回答一些问题。 您可以根据自己的选择回答这些问题,也可以按Enter键安全地跳过它们。 完成后,yarn init命令将创建package.json文件,其中包含您之前提供的答案。 您也可以在稍后阶段手动编辑package.json文件中的信息。
Sometimes you need to add packages in your yarn project to fulfill the project dependencies. To do that, use the yarn add command followed by package name.
有时您需要在yarn项目中添加程序包来满足项目依赖性。 为此,请使用yarn add命令,后跟软件包名称。
# yarn add [package_name]
It is also possible to add specific version of the package with yarn add command by appending package version number or tag:
也可以通过添加软件包版本号或标签,通过yarn add命令添加特定版本的软件包:
# yarn add [package_name]@[tag_or_version]
If you want to remove an installed package, just use the yarn remove
command followed by the package name.
如果要删除已安装的软件包,只需使用yarn remove
命令,然后加上软件包名称即可。
# yarn remove [package_name]
To install all dependencies specified in the package.json file, just use yarn or yarn install. This is useful in situation when you have manually added the dependencies in package.json file and subsequently, want to install them.
要安装package.json文件中指定的所有依赖项,只需使用yarn或yarn install。 当您在package.json文件中手动添加依赖项并随后要安装它们时,这很有用。
# yarn install OR# yarn
That’s it! You have now installed YARN in your Ubuntu 18.04 system and learned a few commonly used yarn command. To know more about yarn usages, visit the official of yarn.
而已! 您现在已经在Ubuntu 18.04系统中安装了YARN,并了解了一些常用的yarn命令。 要了解有关纱线用法的更多信息,请访问纱线的官方 。
翻译自:
转载地址:http://ijlzd.baihongyu.com/