Installing and Using Yarn
You'll learn how to set up Yarn on your machine and use its essential commands to manage packages in your JavaScript projects.
What Is Yarn Setup?
Now that you understand why Yarn exists as an NPM alternative, it's time to actually use it. Installing Yarn and learning its basic commands mirrors what you already know from NPM, but with some syntax differences and performance improvements.
Installing Yarn
Yarn Classic (v1) can be installed globally via NPM itself:
npm install --global yarn
Verify installation:
yarn --version
Yarn Berry (v2+) uses a different approach where you enable it per-project, but for now, Classic is simpler to start with.
Core Yarn Commands
Once installed, Yarn provides familiar package management operations:
Initialize a project:
yarn init
Install all dependencies:
yarn
# or explicitly: yarn install
Add a new package:
yarn add lodash
yarn add --dev jest
Remove a package:
yarn remove lodash
Update packages:
yarn upgrade
Notice the syntax differences: yarn add instead of npm install, plain yarn instead of npm install, and no uninstall keyword.
The Yarn Lockfile
Yarn creates yarn.lock (similar to package-lock.json). Both serve the same purpose—reproducible installs—but with different formats. Keep this file committed to Git.
Key Takeaway: Yarn installs via NPM, uses familiar concepts with different command syntax (yarn add vs npm install), and creates its own lockfile format for dependency consistency.