Package management for the web has come a long way, there are many package managers like bower, npm etc. Of late, Yarn Package Manager has gained popularity going by the google search trends.
What is Yarn and Why is it So Popular ?
It doesn’t mean that Yarn is going to completely replace NPM but Yarn is a new CLI that fetches modules from npm registry. Nothing about the registry will change, you will still be able to fetch and publish packages as usual.
Before looking into the reasons why Yarn is getting so much attention, let’s have to look at the shortcomings of npm.
Problems with NPM:
I have listed below the drawbacks faced by Facebook and Google team. Chances are you’ve never encountered these problems with npm. We are going to compare npm and yarn, so you can decide which one is best for you.
- Queued Installation: While getting dependencies from its repository, npm installs it one after another which takes up a lot of time.
- Single Registry: NpmJS has lot of packages, but if the package you’re looking for is not in NpmJs, then you have to use a Cdn or other options
- No Offline Installation: Npm doesn’t support offline installation as it doesn’t cache packages while installation. This means if you have installed an application before and try to install the same application offline, Npm gives an error
Yarn vs Npm – The Differences:
Yarn looks similar to Npm at first glance, but when you look closer and try using it, you will understand what makes yarn a great tool.
- Parallel Installation: While getting the packages from registry Npm and Yarn carry out a number of tasks. In Npm these tasks are done one after another. Yarn executes these tasks in parallel and reduces the installation time.
Let’s compare the time Yarn and Npm take to install the same package. Before comparing you need to install Yarn globally, by using the below mentioned command.
npm install -g yarn
Yes, I know it feels like using internet explorer to download Google Chrome 🙂
I will be installing express and gulp to compare the time difference between npm and yarn, you may try installing any package you like.
- Yarn: 32 seconds
- Npm: 52 seconds
I know the difference for 2 packages is not much (20 sec) but imagine a project with a number of packages and the amount of time Yarn can save you. The bottomline, Yarn is faster.
- Yarn lock file: Imagine a scenario where you have created an app and it’s working as expected, but after a while the app crashed due to package install version mismatch. To avoid this, yarn creates a lock file with all packages version when installed and updates the same on package changes.
npm shrinkwrap command generates npm lock file and npm will read it before installation. The major difference here is that yarn creates and updates the lock file automatically but npm updates lock file only if it exists.
- Clear Output: If you run
npm installcommand npm will list all installed packages, on the other hand yarn will display less commands like resolving packages, fetching packages, linking dependencies, building fresh dependencies with emojis (except for windows users) 🙁
Yarn Cli Commands:
There are two ways to install yarn, if you haven’t already done it. First is using the npm command below and second is from the official download page.
npm install -g yarn
Note: Both methods need NodeJs already installed on your machine.
Yarn does not intend to replace npm as mentioned earlier. It uses the same package manager to install dependencies and save them to node_modules folder.
Initializing package.json using Yarn:
If you need new package.json file to initialize the dependencies tree you can use the command given below.
If you have used npm earlier, then this command will look familiar. This command will ask a similar question, that npm asks while initializing package files like name, version, description etc.
npm install command will install all the dependencies listed in the package.json file and also allow you to add other packages, However, with yarn there is a slight difference,
yarn install command will only install packages from yarn lock file or package.json file. For adding or upgrading packages there is a different command in yarn.
Adding, Upgrading, and Removing Dependencies:
Adding: Similar to
npm install [package name], the command
yarn add [package name] allows you to add and install dependencies. As the name of the command implies, it adds dependencies, meaning it adds the reference of package in yarn lock and package.json file.
Yarn add command adds the dependencies to dependency section, if you need to add dependencies to the devdependency section you need to add
--dev flag to the command.
Upgrading: This command
yarn upgrade is similar to
npm update and will update all the packages to their latest version. If you specify the package name and version number it will upgrade the package to that specific version.
yarn upgrade email@example.com will update gulp to version 3.9.1.
Removing: This command will remove the package from node_modules folder and update the same in package.json and yarn lock file.
yarn remove [package name].
yarn remove gulp will uninstall gulp and update the same.
Stability and Future:
There were some issues at the time of Yarn launch but the rate of resolved issues is astounding. It indicates that the community is working hard to find and resolve the bugs. Yarn appears stable for most users, including yours truly. I have been using yarn for more than two and a half months and haven’t faced any issue.
The future looks bright, as it is backed by Google and Facebook. Yarn will get actively developed which will make it the default package manager or get forked and used as the official npm package manager.
But nonetheless, the project looks very promising and I would definitely recommend trying Yarn on single projects.
In the final analysis, Yarn scores way higher than npm. We are getting fast package installation, lock file and much more. You can try it on a pilot project and see if it works for you. Atleast give it a shot, it’s worth a try.
And for those of you, using yarn already in your projects please share your experience with Yarn so far. Let me know your take in the comments below.
Follow me on Twitter