For many web developers , their package manager is an indispensable tool: it simplifies their development workflow and unifies their work experience. Among these managers, npm has been reigning supreme for quite some time: it is currently the golden standard for package management, signified by the popularity of the “npm install” search queries.
However, npm does have its drawbacks. To address npm’s issues, other package managers were created. One of them is Yarn , which Facebook, Exponent, Google, and Tilde created to improve upon the package management workflow. In this article, we’ll explore Yarn thoroughly and answer these questions: what are Yarn’s key features? What advantages and drawbacks does Yarn have? How is Yarn workflow organized? Let’s find out!
At first, the devs tried to scale the npm client: only checking
package.json and encouraging developers to manually run
npm install. Even though it was a good practice, it failed to adequately perform in Facebook’s continuous integration (automating the running of code, tests, and/or builds on a separate machine) environments — these environments were meant to be sandboxed for security reasons.
At some point in time, the teams had to conclude that npm wasn’t working for them, so they chose to developer their own solution instead. Teaming up with software engineers from Exponent, Google, and Tilde, they quickly discovered that the type of problems they were experiencing was quite common — and so Yarn was born.
When talking about one package management solution, its competing counterparts are inevitably mentioned: so when we want to discuss Yarn’s key features and differences, we often have to compare them against those of npm. Like in many heated discussions that follow the “A vs. B: What’s Better?” pattern (our recent article about React vs. Angular is a good example, have you read it?), developers like to compare Yarn and npm directly, all the while trying to determine the ultimate winner. However, our experience favors a different approach: balancing web developer tools.
Although Yarn and npm perform the same function, there are certain areas where one gets the upper hand over the other. So what’s great about Yarn?
Speed. Caching every downloaded package, it avoids the need to re-download them later. Additionally, Yarn maximizes resource utilization via concurrent processes, allowing for faster installs.
Reliability. Thanks to the lockfile format and a deterministic manner of installing operations, Yarn ensures baseline installation across all systems.
Security. The integrity of every installed package is verified via checksums — this is done before any package code is executed.
- Support for both npm and bower workflows, allowing to mix registries
- Users can restrict licenses of installed modules…
- … and output license information
- Easy-to-read CLI output
- Offline mode allows for re-installation of packages without an internet connection.
- Unified installation structure independent from installation order.
- Improved network performance via queuing requests in an efficient manner (and avoiding request waterfalls altogether)
- Improved network resilience via preventing individual failed requests from stopping the entire installation; instead, failed requests are automatically retired.
- Elimination of duplicates via resolving mismatched versions of dependencies to a single version.
- More emojis. Probably should’ve put it at the top of the list…
How to install? Although a classic command like
npm install -g yarn can be used for installation, the Yarn team advises against it: it provides separate installation methods for various operating systems. Then, we can finally use the
yarn command in the shell: if not given any arguments, this command will read the
package.json file, fetch packages from the npm registry, and fill the node_modules folder. In essence, this command is equivalent to
How to manage packages? Similar to npm, Yarn logs the dependencies in the
package.json file (located in the project’s root folder), while the dependencies files themselves are stored in the
How to initialize a new project? Running
yarn init will call an interactive prompt that will guide us through the project’s initial set-up:
question name (testdirectory): just-another-package question version (1.0.0): question description: It’s just a test package. It’s not much but it’s honest work. question entry point (index.js): question git repository: https://github.com/yarnpkg/just-another-package question author: Soshace question license (MIT): question private: success Saved package.json
How to inspect licenses? With
yarn licenses ls, you can see the licenses of all project dependencies. It can also generate a disclaimer with yarn
How to inspect dependencies? With
yarn why, you can learn why a specific package was installed.
Still, Yarn can sometimes feel limiting or even subpar in certain areas. While it strived to improve upon the insufficiencies of npm, it happened to create some problems of its own: disk space usage, for instance.
node_modules directory, clogging up disk space with various test files, example directories, and build scripts. Some of these “miscellaneous” files can be cleaned with the clean command:
yarn clean. This creates a
.yarnclean file which defines the file types to be deleted. These are the default settings — Yarn will delete these files and categories:
# test directories __tests__ test tests powered-test # asset directories docs doc website images assets # examples example examples # code coverage directories coverage .nyc_output # build scripts Makefile Gulpfile.js Gruntfile.js # configs .tern-project .gitattributes .editorconfig .*ignore .eslintrc .jshintrc .flowconfig .documentup.json .Yarn-metadata.json .*.yml *.yml # misc *.gz *.md
As long as the
.yarnclean file stays in the project root directory, Yarn will run a cleaning task after every install — and inform how much space it saved.
Although Yarn doesn’t boast the same advantages over npm (as it did back in 2016-2017, before npm version 5), it’s still a very solid choice for web developers. Now that Yarn and npm are becoming ever so similar, the developers can finally appreciate both of these tools and use either of them accordingly.
The post Yarn Package Manager in 2019: Should We Keep on Comparing Yarn with Npm? appeared first on Hire Professionals or Get Remote Job.