Yarn
Yarn is a JavaScript package manager developed by Facebook, and it’s used to manage project dependencies. It was created as an alternative to npm (Node Package Manager), with a focus on speed, security, and consistency.
🔧 What Yarn Does
Just like npm, Yarn helps you:
- Install packages (libraries/modules)
- Manage versioning of those packages
- Handle dependency resolution (which versions of a package should be used)
- Run scripts (like
yarn start,yarn build, etc.)
🆚 Yarn vs npm
Here’s how Yarn stands out (especially when it was first introduced):
- Faster installs: Uses parallel operations and caching to speed things up.
- Lockfile (
yarn.lock): Ensures the exact same versions of dependencies are installed across every machine. - Offline installs: Because of caching, Yarn can install packages even when you’re offline.
- Deterministic dependency resolution: It always installs the same dependencies in the same way across every environment.
🧪 Common Commands
| Action | Yarn Command | npm Equivalent |
|---|---|---|
| Initialize project | yarn init | npm init |
| Add a package | yarn add package | npm install package |
| Remove a package | yarn remove package | npm uninstall package |
| Install all deps | yarn | npm install |
| Upgrade a package | yarn upgrade | npm update |
📦 Yarn 2+ (aka Berry)
Yarn introduced Yarn 2 and later versions (codenamed Berry), which brought some big changes:
- Plug’n’Play (PnP): A new approach to module resolution that eliminates
node_modules - Workspaces improvements: Better support for monorepos
- Zero installs: All dependencies can be committed to the repo
Yarn 2+ is a bit more opinionated, and not all projects are ready for it out of the box, so some teams still stick to Yarn 1.
If you’re already using npm, you can usually switch to Yarn just by running:
npm install --global yarn