Npm To Yarn Cheat Sheet



In a previous post we lookedat Node.js and its package manager NPM. However, NPM has several shortcomingsin terms of performance and reproducibility.

  1. Convert Npm To Yarn
  2. Switch From Yarn To Npm

Facebook’s Yarn is a JavaScript package manager, which resolves NPM’s problems and is a bit more user-friendly.It parallelises better and caches all installed modules.Most importantly, it works with NPM’s repositories and preservesthe structure of the package.json file which makes it easy to migrateexisting NPM projects.

  • Read writing about NPM in Red Shift. The official Infinite Red publication for web & app design, iOS & Android development, React Native, Elixir, JavaScript, and remote work. We’re a fully distributed team building world-class apps for over 20 years for clients all around the world.
  • Here’s a cheat sheet you can use as a handy reference for npm & Yarn. There’s a lot of.

In this post, we will review Yarn’s basic commands and use cases.

# Adds warning to those that install a package of old versions npm deprecate PACKAGE@'npm update -g PACKAGE # Check for outdated packages npm outdated PACKAGE 0 Comments for this cheatsheet.

Yarn can be installed as a global NPM module:

This will give us the yarn command on our terminal path. Starting anew NPM/Yarn project is as easy as:

If you already have an existing NPM project with a package.json, there is no need to initialise Yarn. Yarn commands will work right away.

Given an existing NPM/Yarn project, you can install all packages as:

This is similar to npm install but achieves two additional things. Firstly, it caches the new modules outside of /node_modulesso subsequent installations can be faster.Secondly, it creates the yarn.lock file with the exact versions of all usedpackages. In other words, even if a module has a range of version in itspackage.json, subsequent yarn commands will only use the version specified in yarn.lock. This is similar to the role of Gemfile.lockin Ruby’s package manager Bundler.

Adding and removing new packages/modules is similar to NPM, but by defaultYarn saves all new modules in the package.json. This is equivalent tothe using the --save flag in NPM:

Each of thes commands modifies the yarn.lock and package.json filesand populates the Yarn cache.

Working with global packages is pretty easy as well:

Npm To Yarn Cheat Sheet

Convert Npm To Yarn

Yarn allows you to easily upgrade and downgrade packages. It has a special command for inspecting your dependenciesand identifying if any of them is outdated:

To upgrade/downgrade a package, we can simply use:

The upgrade command modifies both the yarn.lock and package.json files.

Yarn keeps all modules it installed in cache outside of the /node_modulesfolder. When needed, these modules can be quickly retrieved from the cache.For the most parts, we should not be concerned with the caching at all.However, in some rare occasions (e.g. an internal module changed without increasing its version) we may need to clean the cache:

We can specify scripts in the package.json file like this:

With NPM, we could run them with the npm run command. Yarn allows for this as well:

Here are some nice resources on Yarn:

Switch From Yarn To Npm

Please enable JavaScript to view the comments powered by Disqus.