Yarn or Not to Yarn?

Damaris Göbel
3 min readMar 31, 2021
Photo by Emma Louise Comerford on Unsplash

I started coding in 2020 and used npm only. I had a few co-workers who seemed to be all using yarn and then I asked myself, why and what is the difference and what should I use in the future?

I understood that npm and Yarn do the same thing. Here is my little research to this topic. npm and Yarn are package managers for JavaScript.

npm (short for Node Package Manager) is the default package that is automatically installed whenever you install Node.js on your system.

Yarn was developed by Facebook and tried to improve performance and security shortcomings of working with npm. There is a newer version of Yarn, Yarn 2, which has been highly criticized and isn’t compatible with many projects. Here in this article we talk about Yarn 1.

In terms of downloads npm have had more downloads in the last five years.

https://www.npmtrends.com/npm-vs-yarn

npm is automatically installed when you get Node.js. For yarn you have to install through the npm package manager, look here:

npm install --global yarn

https://classic.yarnpkg.com/en/docs/install#mac-stable

They both provide a package.json file, that holds all relevant metadata like dependencies versions, scripts, etc.

All dependencies are stored into the node_modules folder.

Both packages provide a lock file, in yarn it is called yarn.lock, in npm it is called package-lock.json.

This file locks the dependencies to their fixed versions during installation process.

Usage of both in a project

It is recommended to not use npm and Yarn together. It can create conflicts, due to resolution inconsistencies arising from unsynchronized lock files.

Performance

Yarn is still stronger performance wise, but npm is improving its performance with every new release.

Refering to this article https://shift.infinite.red/yarn-1-vs-yarn-2-vs-npm-a69ccf0229cd, Yarn is still faster in terms of installing packages and reinstallation

CLI commands

A lot of commands are the same, but there are some little differences.

For installing dependencies you use npm install or just simply yarn.

For installing packages in npm you run npm install package name, in yarn you run yarn add package name

The command npx which is used in running packages without downloading is not available in yarn.

Conclusion

There is not a lot of difference between these two. Both packages are very close in functionality, just having a few little twists.

I will use yarn from now on as the performance is still stronger at the moment.

--

--

Damaris Göbel

I like to do crazy things with CSS & JavaScript. My brain occasionally runs out of memory so I need to write down my thoughts.