mej

Yarn, a new client for npm

November 13, 2016

Package management has again taken a step forward. Yarn is a new client for npm to make the development usage faster and more secure without any hassle. Yarn is an open source project that is developed in collaboration between Facebook, Exponent, Google, and Tilde.

Often when developing Node.js project's you download over and over again the same packages. You have just downloaded the same set and you need to wait again for the dependencies. Yarn fixes this issue by caching the packages locally on your machine. On top of that, it downloads the packages asynchronously, so the waiting time gets shorter. Yarn also gives added security by checking that the packages checksums are valid.

Yarn uses the flat way of installing the dependencies like the newer npm clients. This way we get rid of duplicates and we actually decrease the node_modules size. For example this project's node_modules files decrease from 15 172 to 6 911 files. The flat directory structure is also better for Windows file system, that doesn't like long directory paths.

You should already use yarn for most the projects that you have package.json for. Yarn doesn't yet work with private packages, but otherwise it has a good set of features already. More about migrating from npm to Yarn.

Yarn is a drop in placement for npm client. Easiest way to install it is:

npm install -g yarn

If you don't have Node.js already installed, I suggest you read the official install guides.

Here are some of the most common commands that get you going.

Starting a new project

yarn init

###Adding a dependency

yarn add [package]
yarn add [package]@[version]
yarn add [package]@[tag]

Updating a dependency

yarn upgrade [package]
yarn upgrade [package]@[version]
yarn upgrade [package]@[tag]

Removing a dependency

yarn remove [package]

Installing all the dependencies of project

yarn

or

yarn install

This command creates yarn.lock that you should include in your repository.