Installing and Using Yarn Package Manager for Node.js
What Will You Learn in This Guide?
In this guide, you learn the global installation of Yarn, setting the project-specific Yarn Berry version, using dependency add/remove/update commands, and the Yarn–npm differences in detail.
🛠️ 1. Node.js Control and Global Yarn Installation
Check That Node.js Is Installed
This command shows whether Node.js is installed on the system.
node -v
Installing Yarn CLI Globally
This command installs Yarn's global command line tool. This is what starts the native Yarn that will be used in the project.
sudo npm install -g yarn
Verify Global Yarn is Installed Correctly
This command shows the global Yarn version (usually 1.22.x).
yarn --version
📂 2. Setting Up a Project-Specific Yarn Berry Version
Creating a Project Directory
To create a directory if you're starting a new project:
mkdir genixnode-app
cd genixnode-app
Installing Project-Specific Yarn Version
This command installs Yarn Berry (modern version) into your project and creates the .yarn/releases folder.
yarn set version berry
Check Local Yarn Version Within Project
This command now shows the Yarn 3.x / 4.x version specific to your project.
yarn --version
🧾 3. Configuring .gitignore for Git
The following structure excludes Yarn's unnecessary folders and includes the necessary ones.
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
.pnp.*
⚙️ 4. Basic Yarn Commands (Explained)
Starting a New Project
It creates package.json and yarn.lock in your current directory.
yarn init
Installing All Dependencies
Installs all modules in package.json.
yarn install
Adding a New Package
It installs a dependency and updates the package.json + yarn.lock files.
yarn add express
Add Development Only Dependency
This command adds the dependency to devDependencies.
yarn add eslint --dev
Dependency Removal
Removes a package from the project.
yarn remove express
Update All Dependencies
It updates according to the version ranges in package.json.
yarn upgrade
Running Scripts
It runs a command in the scripts section in package.json.
yarn run test
🧪 5. Simple Server with Express (Explanated Example)
Express installation
yarn add express
index.js – Simple HTTP Server
const express = require("express");
const app = express();
// Root endpoint yanıtı
app.get("/", (req, res) => res.send("Yarn is working!"));
// Sunucuyu port 3000'de başlat
app.listen(3000, () => console.log("Server running on http://localhost:3000"));
Running the Server
yarn node index.js
Testing the Server
curl http://localhost:3000
🧩 6. Yarn vs NPM: Detailed Comparison
| Feature | Tomorrow | NPM |
|---|---|---|
| Installation Speed | Very fast, parallel processing | Slower |
| Consistency | Strictly deterministic (yarn.lock) | Flexible |
| Monorepo | Workspaces powerful | Limited |
| Disk Usage | Uses 50% less space with PnP | node_modules heavy |
| Working Offline | Powerful cache | Weak |
| Configuration | .yarnrc.yml | .npmrc |
💡 7. When to Use Yarn?
-
If consistency is required in CI/CD environments
-
In large monorepo structures
-
If you want faster installation
-
If you want to save disk space thanks to PnP
When to Use NPM?
-
In simple projects
-
If the team culture is based on NPM
-
If your toolchain only supports NPM
❓ 8. Frequently Asked Questions (FAQ)
1. What does PnP mode do?
Removes node_modules folder, decompresses all modules from .pnp.cjs file. Provides speed and disk saving.
2. Should project Yarn 4.x be different from Global Yarn 1.x?
Yes. Global Yarn is just a "launcher". Berry always works within the project.
3. How do I convert an NPM project to Yarn?
Enter the project →
yarn install
→ yarn.lock is created → package-lock.json is deleted.
4. What causes the “Packages Not Found” error with PnP?
Because older tools expect node_modules. Solution:
yarn dlx @yarnpkg/sdks vscode
5. What does yarn install --immutable do?
If there is a difference between package.json and lockfile in CI/CD, it stops the installation.
🧾 Result
With this guide, you learned about Yarn's global installation, in-project Berry version fixing, basic commands, PnP advantage and Yarn–npm differences. For speed, consistency, and modern monorepo needs, Yarn is a strong choice.
You can easily manage your projects with Yarn on the GenixNode infrastructure.

