Learning Center
Node.js

How to Install and Use Yarn Package Manager for Node.js

10 Feb 2025
Hostman Team
Hostman Team

Yarn is an efficient tool for managing dependencies in Node.js-based projects. It is known for its high speed, security, and ease of use.

What is Yarn and Why Use It?
Copy link

Yarn is an alternative to the standard npm (Node Package Manager). It is designed to handle packages and projects built on Node.js. Yarn offers several advantages over npm:

  • Speed: Yarn downloads packages in parallel, significantly reducing installation time.
  • Security: The use of a yarn.lock file helps prevent version conflicts.
  • Deterministic Builds: Ensures identical package versions across different machines.
  • User-Friendly Interface: Cleaner command syntax and additional tools for dependency management.

If your project involves working with many packages and dependencies, using Yarn can greatly simplify the task. It allows for faster and more secure package installations while making dependency management more predictable — a valuable benefit for team-based projects.

Comparison of Yarn and npm
Copy link

Yarn's advantages make it particularly appealing for developers, especially in large-scale projects.

Feature

Yarn

npm

Installation Speed

Faster thanks to caching

Slower

Dependency Handling

Deterministic builds

Potential version conflicts

Lock File

yarn.lock

package-lock.json

Ease of Use

Simplified syntax

More standard interface

Installing Yarn
Copy link

Before installing Yarn, ensure that Node.js and npm are installed:

  1. Open the terminal or command prompt.

  2. Run the following commands to check the versions of Node.js and npm:

node -v
npm -v

If Node.js or npm is not installed, download them from the official Node.js website. You may also find our installation guide helpful.

  1. To install Yarn globally, run:

npm install -g yarn
  1. Check if Yarn was installed successfully:

yarn --version

If the command returns the version number, Yarn has been installed correctly.

Yarn Commands
Copy link

Yarn's intuitive syntax makes it easy to manage your project dependencies efficiently.

Project Initialization
Copy link

To get started with Yarn, initialize your project to create a package.json file containing project and dependency information.

  1. Navigate to your project directory:

cd your-project-directory
  1. Run the following command and follow the prompts:

yarn init

This will generate a package.json file with basic project settings.

Installing Packages
Copy link

To install a single package:

yarn add <package-name>

This adds the specified package to your project.

To install a package as a development dependency:

yarn add <package-name> --dev

This is useful for packages required only during development.

To install a specific version of a package:

yarn add <package-name>@<version>

This allows you to select the desired package version.

Installing All Dependencies
Copy link

If the project already contains a package.json or yarn.lock, run:

yarn install

This is helpful when cloning a project from a repository to quickly set up the environment.

Removing Packages
Copy link

To remove a package from your project and update package.json, use:

yarn remove <package-name>

Updating Dependencies
Copy link

To upgrade packages to their latest versions, run:

yarn upgrade

This ensures your project uses the most current versions.

Dependency Security Audit
Copy link

To identify vulnerabilities in your project dependencies:

yarn audit

This helps detect and address potential security threats.

Caching
Copy link

Yarn leverages caching to speed up subsequent package installations.

To clear the cache:

yarn cache clean

This command can be useful if you encounter issues during package installation.

Conclusion
Copy link

Yarn is a modern tool for managing dependencies in Node.js projects. Its speed, security features, and intuitive interface make it an excellent choice for developers.