How to Get Started with the Yarn Package Manager for Node.Js

By Maz Ahmadi · December 23, 2016


Yarn is an open-source dependency manager for Node.js. It is developed by Facebook, Exponent, Google and Tilde. Yarn aims to solve challenges that the npm client creates at scale. It claims to be faster and more reliable than npm client. Yarn also verifies the integrity of all modules and their relative dependencies prior to code execution.

Install Yarn

If you have npm already installed, you can just use it to install Yarn globally. If not, please see this article on other ways to install Yarn.

$ npm install -g yarn

Migrate an existing project

They couldn’t have made it any easier!

Simply navigate to your project’s root folder (the directory where your package.json file is located) and run the command below.

$ yarn

Create a new project

To get your new Node project up and running with Yarn, create a new directory for your project and navigate to it.

$ mkdir my-project

$ cd my-project/

If you’re already familiar with npm, Yarn’s initialize command works the same way.

$ yarn init

Yarn will take you through a list of questions regarding your project. When in doubt, just press Enter for the default value that is presented in parentheses.

After completing the final question, the console should print “success Saved package.json.

You now have a package.json file for your Node project.

Add a module

To add modules to your project, simply run the command below. Just replace [module] with the name of the dependency that is registered to the npm package.

$ yarn add [module]

Yarn will add the module and its relative dependencies into the node_modules folder. It will also update the package.json and yarn.lock files.

Remove a module

If you no longer need a dependency, it is a good idea to remove it from the node_modules folder, the package.json, and yarn.lock files.

Luckily, Yarn gives us the ability to remove all three using a single command.

$ yarn remove [module]

Install all modules

To install dependencies that are listed in a package.json file, simple run one of the commands below from the project’s root directory.

$ yarn

OR

$ yarn install

Upgrade a module

To upgrade a single module to the latest version, simply run the following command.

$ yarn upgrade [package]

To specify a specific version, you can use the command below.

$ yarn upgrade [package]@[version]

Version Control

Since the node_modules directory is not usually committed to version control, be sure to commit the package.json and yarn.lock files. The yarn.lock file contains important information regarding the install process. This file ensures that dependencies stay consistent across different systems and builds.

Conclusion

Yarn has made it incredibly easy to manage Node.js dependencies. It is important to note that Yarn is compatible with major continuous integration platforms including AppVeyor, CircleCI, Codeship, and Travis CI.

If you have any question, let me know in the comments! Otherwise, take a look at the Yarn docs for more information regarding additional features.

Cheers!

Leave a Reply