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.
$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.
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.
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.
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.
{ "name": "example-app", "version": "1.0.0", "private": true, "dependencies": { "lodash": "^4.17.21" } }
When you run:
Execute this command in your project's root directory to install its declared dependencies.
$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.
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.
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.
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.
$ cd your-project
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.
$ yarn --version
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.
$ yarn install
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.
node_modules/
Traditional dependency directory
.pnp.cjs
Plug'n'Play resolution file
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.
$ yarn test $ yarn build
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.
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.
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.
Link
In the linking phase, Yarn prepares the dependency structure using the project's configured linker. This determines how installed packages will be made available to the application and its development tools.
Linker options:
The traditional node-modules linker
creates a node_modules
directory. Plug'n'Play instead uses
generated resolution information, commonly
through .pnp.cjs, so compatible
tools can locate packages without a
conventional dependency directory.
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.
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.
{
"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:
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.