Yarn Package Manager Installation and Usage (Node.js)
What will you learn in this guide?
This guide explains how to install and use the Yarn package manager effectively for Node.js projects.
You will learn global and project-based Yarn installation, basic commands and its differences with npm.
Technical Summary
This guide explains how to install and use the Yarn package manager in the Node.js environment.
The goal is to manage dependencies more quickly, consistently and predictably.
Steps: global installation → project-based configuration → basic commands → npm benchmark.
Preliminary Preparations
- Node.js must be installed (LTS recommended)
- You must be familiar with using terminal
- You must have a Node.js project folder
Check Node.js version:
node -v
- This command shows whether Node.js is available on your system.
1. Installing Yarn Globally
- Yarn is first installed throughout the system.
- This installation is used to launch the Yarn version within the project.
sudo npm install -g yarn
- This command installs the Yarn CLI globally.
Verify installation:
yarn --version
- This command shows the installed global Yarn version.
2. Setting a Project-Specific Yarn Version
- It is important to use the same Yarn version in every project. This approach ensures team and CI/CD consistency.
Enter the project directory:
cd ~/my-project
- Set Yarn Berry version:
yarn set version berry
- This command downloads and configures the project-specific Yarn version.
Check version again:
yarn --version
- This command now shows the Yarn version within the project.
3. Starting a New Yarn Project
- Initial files for a project are created from scratch.
yarn init
- This command creates package.json and yarn.lock files.
4. Installing Dependencies
- To install all dependencies in an existing project:
yarn install
- This command performs an exact installation according to the yarn.lock file.
5. Adding and Removing Packages
- To add a new package:
yarn add express
- This command downloads the package and updates the lock file.
To remove a package:
yarn remove express
- This command completely deletes the package from the project.
6. Quick Verification (Test)
- Test Yarn by setting up a simple Express server.
const express = require("express");
const app = express();
app.get("/", (req, res) => res.send("Yarn çalışıyor"));
app.listen(3000);
- This code starts a simple HTTP server on port 3000.
Run:
yarn node index.js
- This command starts the application via Yarn.
7. .gitignore Configuration
- Yarn requires adding some files to Git.
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
.pnp.*
- This build preserves the necessary Yarn files.
8. Additional Useful Yarn Commands
- Listing packages:
yarn list --depth=0
Checking for updates:
yarn outdated
Clear cache:
yarn cache clean
Tomorrow or npm?
- Yarn and npm serve similar purposes, but they have differences:
| Feature | Tomorrow | npm |
|---|---|---|
| Setup Speed | Faster | Medium |
| Consistency | Very high | High |
| Monorepo | Strong | Limited |
| Offline Usage | Strong | Weak |
| PnP Support | Yes | None |
Summary:
Yarn is more suitable for large teams and monorepo structures. For simple projects, npm is sufficient.
Frequently Asked Questions (FAQ)
1. Why does Yarn use a two-step installation? The Global CLI is just a starter. Provides version consistency within the project.
2. Yarn doesn't create node_modules? It doesn't create in default PnP mode. It can be turned off if desired.
3. Can Yarn work offline? Yes. Packages can be loaded from cache.
4. Is Yarn suitable for CI/CD? Yes. The --immutable option forces an exact installation.
5. Can I switch from npm to Yarn? Yes, but package-lock.json should be deleted.
Result
Yarn provides speed, consistency and scalability in Node.js projects. It offers a strong advantage, especially in teamwork and CI/CD processes.
You can try it now on the GenixNode platform to run your Node.js projects on a secure and high-performance infrastructure.

