未加星标

5 things you can do with Yarn

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

TL;DR:There are several package managers in the javascript land: npm , bower , component , and volo , to name a few. As of this writing, the most popular JavaScript package manager is npm . The npm client provides access to hundreds of thousands of code libraries in the npm registry. Just recently, Facebook launched a new package manager for JavaScript called Yarn , which claims to be faster, more reliable, and more secure than the existing npm client. In this article, you will learn five things you can do with Yarn.

Yarnis a new package manager for JavaScript created by Facebook. It offers a fast, highly reliable, and secure dependency management for developers using JavaScript in their apps. Here are five things you can do with Yarn.

1. Work Offline

Yarn offers you the ability to work in offline mode. If you have installed a package before, you can install it again without an internet connection. A typical example is shown below:

When connected to the internet, I installed two packages with Yarn like so:


5 things you can do with Yarn
Create a package.json with yarn init
5 things you can do with Yarn
Install express and jsonwebtoken packages with yarn
5 things you can do with Yarn
Installation complete

After the installation was complete, I went ahead and deleted the node_modules inside my orijin directory and also disconnected from the Internet. I ran Yarn like so:


5 things you can do with Yarn
Yarn installed the packages offline

Voilá! All the packages were installed again in less than two seconds. Apparently, Yarn caches every package it downloads so it never needs to do so again. It also maximizes resource utilization by parallelizing operations so that install times are faster than ever.

2. Install from Multiple Registries

Yarn offers you the ability to install JavaScript packages from multiple registries, such as npm , bower , your git repository, and even your local file system.

By default, it scans the npm registry for your package as follows:

yarn add <pkg-name>

Install a package from a remote gzipped tarball file as follows:

yarn add <https://thatproject.code/package.tgz>

Install a package from your local file system as follows:

yarn add file:/path/to/local/folder

This is particularly helpful for developers who constantly publish JavaScript packages. You can use this to test your packages before publishing them to a registry.

Install a package from a remote git repository like so:

yarn add <git remote-url>
5 things you can do with Yarn
Yarn installs from a Github repo
5 things you can do with Yarn
Yarn also automatically detects that the git repo exists as a package in the bower registry and treats it as such 3. Fetch Packages Speedily

If you have used npm for a while, you must have had experiences where you had to run npm install , then go watch a movie, and come back to check whether all the packages you required are finished installing. Well, maybe not that long, but it takes a lot of time to traverse the dependency tree and pull dependencies in. With Yarn, installation time has really been cut down from having to wait several minutes to package installs happening in seconds.

Yarn efficiently queues up requests and avoids request waterfalls to maximize network utilization. It starts by making requests to the registry and recursively looking up each dependency. Next, it looks in a global cache directory to see whether the package has been downloaded before. If it hasn't, Yarn fetches the tarball package and places it in the global cache to enable it to work offline and eliminate the need to re-download.

During install, Yarn parallelizes operations, which makes the install process faster. I did a fresh install of three packages, jsonwebtoken , express and lodash , using npm and yarn . After Yarn was finished installing them, npm was still installing.


5 things you can do with Yarn
4. Lock Package Versions Automatically

Npm has a feature called shrinkwrap , which is intended to lock down your package dependencies for production use. The challenge with shrinkwrap is that every developer has to manually run npm shrinkwrap to generate the npm-shrinkwrap.json file. Developers are also humans; we can forget!

With Yarn, it's a different ball game. During installation, a yarn.lock file is generated automatically. It is similar to the composer.lock file thatphp developers are familiar with. The yarn.lock file locks down the exact versions of the packages that have been installed and all their dependencies. With this file, you can be certain that every member of your engineering team have the exact package versions installed and deployments can easily be reproduced without unexpected bugs.

5. Install Dependencies the Same Way across Machines

The npm client installs dependencies in a way that can make the structure of the contents of Developer A node_modules directory different from Developer B . It uses a non-deterministic approach to install these package dependencies. This approach is sometimes responsible for bugs that can't be easily reproduced because of the popular works on my system problem.

With Yarn, the presence of a lock file and an install algorithm ensures that the dependencies installed produce the exact same file and folder structure across development machines and when deploying applications to production.

Note:One more thing, I know I promised five but I can't help tell you how good Yarn makes me feel. Enterprise environments require the ability to be able to list a dependencies' license type. Yarn offers the ability to list the license type for a given dependency by running yarn licences ls in your root directory as follows:


5 things you can do with Yarn
Conclusion

Yarn in its infancy has already brought significant improvements in the way JavaScript packages are fetched from global registries into local environments, especially with regard to speed and security. Will it grow to become the most popular choice among JavaScript developers? Have you switched yet? What are your thoughts about Yarn? Let me know in the comments section! :blush:

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: JavaScriptJavaFacebookGitPHP
分页:12
转载请注明
本文标题:5 things you can do with Yarn
本站链接:http://www.codesec.net/view/483710.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(33)