Yarn Install Guide · CorePack

yarn install Dependencies, Flags & Errors

The yarn install command prepares the dependencies needed by a JavaScript or Node.js application. Yarn reads the project's dependency settings, uses the lockfile to resolve package versions, fetches necessary packages, and makes them accessible through the configured installation method.

Quick answer
$yarn install
$yarn install --immutable
$yarn install --frozen-lockfile

The immutable and frozen-lockfile options provide comparable lockfile protection, but their usage differs between modern Yarn and Yarn Classic.

~/projects/example-app
yarn install
$ yarn --version
4.5.0
$ yarn install
→ Resolving packages...
→ Fetching packages...
→ Linking dependencies...
→ Building dependencies...
✓ Done in 2.34s
✓ Saved lockfile
$ yarn check
Project check passed
$

After cloning a repository or pulling changes that introduce new dependencies, yarn install is commonly among the first commands to run. In this guide, you'll learn about dependency installation, configuration files, available flags, CI workflows, and ways to diagnose and resolve common installation problems.

Overview

What does yarn install do?

yarn install sets up the dependencies specified in your project's package.json. It works with the lockfile and Yarn's project settings to prepare the packages your application needs for development, testing, and running.

Example Project

Consider a project with this package.json

A JavaScript project uses package.json to list the packages it depends on. In this example, the application requires lodash.

The dependencies field identifies the packages needed by the application.

{ } package.json
{
  "name": "example-app",
  "version": "1.0.0",
  "private": true,
  "dependencies": {
    "lodash": "^4.17.21"
  }
}
Example package manifest containing a declared application dependency.

When you run:

Execute this command in your project's root directory to install its declared dependencies.

Terminal · bash
$yarn install

Yarn determines which packages are required, including the dependencies of those packages.

Depending on the installed Yarn version and project configuration, packages can be placed inside node_modules or resolved with Plug'n'Play (PnP).

When should you run yarn install?

These are common reasons to run an installation. You typically don't need to repeat the command after every change to your application's source code.

  • After cloning an existing JavaScript project.
  • When a teammate adds or updates project dependencies.
  • After switching Git branches when dependency files have changed.
  • When previously generated installation artifacts are missing.
  • Before testing or building a project in a clean CI environment.
  • Following an intentional update to dependency configuration.
Section 02 / Step-by-Step

How to Run Yarn Install Step by Step

Before installing your project dependencies, confirm that Yarn is accessible from your terminal. Follow these five steps to navigate to your application, check the selected Yarn version, install the required packages, and verify that your project is ready to use.

Before you begin

Yarn must already be installed and available on your system's PATH. If your terminal doesn't recognize the yarn command, set up Yarn or Corepack before continuing.

Corepack Setup Guide
Step 1

Open the Project Directory

Open your terminal and move into the root directory of your application. This directory usually contains the package.json file. Projects already using Yarn commonly include a yarn.lock file as well.

Terminal / bash
$ cd your-project
Step 2

Check the Active Yarn Version

Check which version of Yarn is active for the current project. When Corepack manages the project, it can select the version declared in the packageManager field of package.json. This helps you choose flags compatible with Yarn Classic or modern Yarn.

Terminal / bash
$ yarn --version
Step 3

Install Dependencies

Run the installation command to prepare your project's dependencies. Yarn resolves the required package versions, downloads missing archives, creates the appropriate dependency links, and runs applicable build scripts. The exact installation process and terminal output can vary.

Terminal / bash
$ yarn install
Step 4

Check for Installation Errors

Examine the terminal output for any installation failures. A successful installation prepares the dependency artifacts required by the project. With the node-modules linker, this generally means a node_modules directory; Plug'n'Play projects may generate a .pnp.cjs file instead.

What to check
node_modules/ Traditional dependency directory
.pnp.cjs Plug'n'Play resolution file
Step 5

Run a Project Script

After installing dependencies, try running an appropriate project script. For example, many applications provide test or build commands. These commands work only when the corresponding scripts or supported executables exist in the project. Successful installation doesn't guarantee that every script will succeed.

Check your project's package.json before running its scripts.
Terminal / bash
$ yarn test
$ yarn build
Behind the Scenes

How Yarn Install Works Behind the Scenes

Modern Yarn performs an installation through four main phases: Resolution, Fetch, Link, and Build. Understanding what happens in each phase makes it easier to identify installation problems and troubleshoot errors efficiently.

Phase 1

Resolution

During resolution, Yarn determines which package versions meet the dependency requirements of your project. It considers the version ranges declared in package.json and the package resolutions recorded in yarn.lock.

Important: When the existing lockfile already contains suitable dependency resolutions, Yarn can reuse them. Changes to declared dependencies may require a lockfile update. An immutable installation prevents modifications to that file.

Phase 2

Fetch

During the fetch phase, Yarn obtains package archives that are not yet present in its configured cache. Cached packages can be reused during subsequent installations, helping reduce unnecessary downloads.

Common issues: Package retrieval can fail because of network interruptions, incorrect registry settings, missing authentication, or package versions that are unavailable.

Phase 4

Build

Once the dependency structure is ready, Yarn executes applicable build scripts for packages that require them. Some dependencies include native components or installation scripts that depend on additional system tools.

Build failures: An installation may still fail during this final phase even when Yarn successfully resolved all package versions and downloaded the required dependency archives.

Configuration Files

Understanding package.json and yarn.lock

Two files play a central role in most Yarn projects.

What is package.json?

package.json defines project metadata, direct dependencies, development dependencies, and scripts. Here, lodash is a declared dependency and check is a project script.

{ } package.json
{
  "name": "sample-app",
  "version": "1.0.0",
  "private": true,
  "dependencies": {
    "lodash": "^4.17.21"
  },
  "scripts": {
    "check": "node -e \"console.log('Project check passed')\""
  }
}

After installing dependencies, you can run:

bash
yarn check

The example uses a standard Node.js command, so it doesn't require a framework or additional test runner.

What is yarn.lock?

The yarn.lock file records dependency resolutions selected by Yarn. Suppose package.json allows several compatible releases of a package. The lockfile helps preserve the selected resolution so developers and automated environments don't independently choose different compatible versions every time.

Does yarn install change yarn.lock?

It can. If the existing lockfile fully satisfies the dependency declarations, Yarn generally doesn't need to change those resolutions. If dependencies have been added or changed, Yarn may update the lockfile.

  • → For Yarn Classic, the official documentation states that a complete lockfile is reused without checking for newer releases.
  • → For modern Yarn, immutable installations prevent lockfile modifications.
Scroll to Top