What Is Yarn? A Beginner's Guide
Yarn is an open-source package manager used to install and manage dependencies in JavaScript projects. It helps developers add packages, remove dependencies, run scripts, and organize related projects.
When you build an application with JavaScript or TypeScript, you will often use code created by other developers. Yarn helps you manage that code in a consistent and organized way.
In this guide, you will learn: Yarn installation, common commands, package.json, yarn.lock, scripts, workspaces, and the differences between Yarn and other package managers.
Table of Contents
- Why Was Yarn Created?
- Yarn and JavaScript
- Yarn Versions
- Installing Yarn
- Creating a Project
- Understanding package.json
- Managing Dependencies
- Understanding yarn.lock
- Running Scripts
- Yarn Workspaces
- Caching and Plug'n'Play
- Yarn vs npm, pnpm, and Bun
- Useful Yarn Commands
- Team Development and CI
- Common Mistakes
- Frequently Asked Questions
1. Why Was Yarn Created?
JavaScript applications often rely on many external packages. Managing all these packages manually can be difficult, especially when a project has many dependencies.
Yarn became popular by focusing on reliable dependency installation, lockfiles, caching, and predictable project setup. Modern Yarn continues to provide tools for dependency management and larger multi-package projects.
- Install packages required by an application.
- Keep track of dependency versions.
- Run commands defined by a project.
- Manage multiple packages in a single repository.
- Help developers reproduce a project's dependency setup.
2. Yarn and JavaScript
Yarn is commonly used in the JavaScript and TypeScript ecosystem. It can support projects built with technologies such as:
- Node.js
- React
- Vue
- Angular
- Svelte
- Next.js
- Express
- Vite
- Other JavaScript and TypeScript tools
3. Understanding Yarn Versions
Yarn has two major generations that beginners may encounter:
Yarn Classic
Yarn Classic refers to Yarn 1. It is still found in older projects and uses its own commands and configuration conventions.
Modern Yarn
Modern Yarn refers to the newer releases, including Yarn 2 and later. It supports features such as Plug'n'Play, configurable installation strategies, and advanced workspaces.
You can check your installed version with:
yarn --version
4. Installing Yarn
Yarn is primarily a command-line tool used in a development environment. To work with it, you generally need Node.js or another supported setup appropriate to your project.
Step 1: Install Node.js
Install a suitable Node.js version from the official Node.js website if your project requires Node.js.
Step 2: Check Corepack availability
Corepack is a tool that can select and manage package-manager versions for projects. Its availability and installation method depend on your Node.js distribution and version.
node --version
npm --version
corepack --version
Step 3: Enable or prepare Yarn
If Corepack is available, a typical setup is:
corepack enable
yarn --version
If Corepack is not available, follow the installation instructions for your Node.js version and operating system in the official Yarn installation guide.
5. Creating a New Yarn Project
To create a project configuration in a new or existing directory, use:
yarn init
For modern Yarn, you may also see this command in tutorials:
yarn init -2
Follow the prompts if Yarn asks you to provide project details. The command prepares project configuration, commonly including a package.json file.
Example project workflow
mkdir my-yarn-app
cd my-yarn-app
yarn init
The exact setup may vary with the Yarn version and the project template you choose.
6. Understanding package.json
The package.json file stores project information, scripts, and dependency declarations. Yarn uses this file to understand which packages the project needs.
{
"name": "my-yarn-app",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "node index.js"
}
}
Common fields include:
name: the project or package name.version: the declared package version.private: prevents accidental publishing when set totrue.scripts: named commands that can be run by Yarn.dependencies: packages needed by the application.devDependencies: packages mainly needed during development.
7. Managing Dependencies
Install project dependencies
Use this command to install the dependencies declared for a project:
yarn install
In modern Yarn, running yarn by itself also runs installation.
Add a package
To add a package to your project:
yarn add express
This updates the project's dependency information and lockfile as needed.
Add a development dependency
For a tool mainly used during development, use:
yarn add eslint --dev
Request a specific package version
yarn add express@5
yarn add package-name@1.2.3
The first example requests a compatible version matching the specified major-version range as resolved by the package manager. The second requests version 1.2.3.
Remove a package
yarn remove express
This removes the dependency declaration and updates the installation information.
Upgrade dependencies
Modern Yarn uses yarn up to upgrade dependencies:
yarn up
yarn up express
Review dependency changes and test your application after upgrading. Yarn Classic uses different upgrade command conventions.
8. What Is yarn.lock?
The yarn.lock file records the resolved versions and related information for dependencies. This helps the project install a consistent dependency tree across different development machines and build systems.
For example, your package.json may permit a range of versions. The lockfile records which versions were resolved for the project.
Why is the lockfile important?
- Helps reproduce dependency installations.
- Reduces unexpected version changes between machines.
- Supports consistent automated builds.
- Makes dependency changes easier to review.
9. Running Scripts with Yarn
You can define reusable commands in the scripts section of package.json.
{
"scripts": {
"dev": "node server.js",
"build": "node build.js",
"test": "node test.js"
}
}
Run the scripts from your project directory:
yarn dev
yarn build
yarn test
In modern Yarn, yarn build is a convenient way to run the project's build script. A script works only if it is defined and the required files and tools are available.
10. What Are Yarn Workspaces?
Workspaces allow multiple related packages to be managed inside one project. They are especially useful for monorepos, where a repository contains several applications or shared packages.
Example folder structure
company-project/
├── package.json
├── apps/
│ ├── website/
│ └── mobile/
└── packages/
└── shared/
Example root package.json
{
"name": "company-project",
"private": true,
"workspaces": [
"apps/*",
"packages/*"
]
}
With a compatible workspace setup, Yarn can recognize packages in these folders and manage their dependencies together.
Benefits of workspaces
- Manage several related packages in one repository.
- Share code between applications.
- Link local packages together.
- Centralize dependency installation and project management.
- Organize larger codebases more effectively.
For configuration details, see the official Yarn workspaces documentation.
11. Caching and Plug'n'Play
Offline caching
Yarn caches package data to reduce repeated downloads. Depending on the project configuration and which packages are cached, this can make later installations faster and may help when network access is limited.
Plug'n'Play (PnP)
Plug'n'Play is an installation strategy supported by modern Yarn. Instead of relying on a traditional node_modules directory, it uses generated resolution information to help applications locate dependencies.
PnP can provide a more controlled dependency setup, but some tools may need configuration or compatibility adjustments.
Yarn and node_modules
Many JavaScript tools expect a node_modules directory. Yarn supports installation strategies according to the project's configuration, so check the existing setup before changing it.
12. Yarn vs npm, pnpm, and Bun
Yarn vs npm
Yarn and npm are both JavaScript package managers. They can install dependencies, manage package information, run scripts, and support workspaces.
| Feature | Yarn | npm |
|---|---|---|
| Package management | Yes | Yes |
| Project manifest | package.json | package.json |
| Common lockfile | yarn.lock | package-lock.json |
| Run project scripts | Yes | Yes |
| Workspaces | Yes | Yes |
Choose the package manager that fits your project and follow its documentation. Avoid switching package managers casually because their lockfiles and configuration are different.
Yarn vs pnpm
pnpm is another JavaScript package manager. It uses a content-addressable store and a linking strategy designed to share package files efficiently. Yarn and pnpm differ in installation behavior, configuration, and ecosystem compatibility.
Yarn vs Bun
Bun is a JavaScript runtime and development toolkit that also includes a package manager. Yarn focuses on package management and project workflows. Which tool is suitable depends on your application's requirements and existing tools.
13. Useful Yarn Commands
| Command | Purpose |
|---|---|
yarn --version |
Display the installed Yarn version. |
yarn init |
Initialize project configuration. |
yarn install |
Install project dependencies. |
yarn add package |
Add a dependency. |
yarn add package --dev |
Add a development dependency. |
yarn remove package |
Remove a dependency. |
yarn up package |
Upgrade a dependency in modern Yarn. |
yarn dev |
Run the configured dev script. |
yarn build |
Run the configured build script. |
yarn test |
Run the configured test script. |
yarn install --immutable |
Install without allowing the lockfile to change in modern Yarn. |
Some commands vary by Yarn version. Check the relevant documentation before using a command from an older tutorial.
14. Yarn in Team Development and CI
When several developers work on the same project, consistent dependencies are important. Keeping package.json and the appropriate lockfile in version control helps team members install the same resolved dependency versions.
Continuous integration
Yarn can also be used in automated testing and build systems. Modern Yarn supports the --immutable option:
yarn install --immutable
This makes installation fail if Yarn would need to modify the lockfile. It helps automated builds detect unexpected dependency changes.
15. Common Yarn Mistakes
- Using instructions for Yarn Classic with a modern Yarn project.
- Mixing npm, Yarn, and pnpm without a clear reason.
- Deleting
yarn.lockunnecessarily. - Editing the lockfile manually.
- Ignoring the Yarn version required by the project.
- Upgrading packages without testing the application.
- Assuming every project supports Plug'n'Play.
- Running a script that has not been defined in
package.json. - Ignoring dependency or installation errors.
package.json, and run yarn --version before changing dependencies.
16. A Simple Beginner Workflow
- Install Node.js if your project requires it.
- Set up the Yarn version recommended for your project.
- Create or open the project directory.
- Initialize project configuration if needed.
- Install the declared dependencies.
- Add packages only when the project needs them.
- Write your application code.
- Define scripts in
package.json. - Run the appropriate development, build, and test scripts.
- Keep the lockfile under version control when appropriate.
Example project structure
my-project/
├── package.json
├── yarn.lock
├── index.js
├── README.md
└── .gitignore
This is a simple example. Real projects may contain additional folders, configuration files, or a different dependency installation structure.
Frequently Asked Questions
Is Yarn a programming language?
No. Yarn is a package manager used mainly in the JavaScript ecosystem.
Is Yarn only for Node.js?
Yarn is commonly used with Node.js-based development tools and JavaScript or TypeScript projects. Its exact role depends on the project's tooling.
What is yarn.lock?
It is a lockfile that records resolved dependency information to help make installations consistent.
What does yarn add do?
It adds a package to the project's dependencies and updates the relevant project files.
What does yarn install do?
It installs the dependencies specified by the project and uses its lockfile where applicable.
What are Yarn workspaces?
Workspaces let you manage multiple related packages together in one project, which is useful for monorepos.
Can Yarn replace npm?
Yarn is an alternative package manager for many JavaScript projects. However, changing package managers requires attention to lockfiles, scripts, and project configuration.
Is Yarn free?
Yes. Yarn is an open-source project.
Do I need Yarn to learn JavaScript?
No. You can learn JavaScript without Yarn. However, learning a package manager becomes useful when you start working with projects that use external packages and development tools.
Where can I learn more about Yarn?
Visit the official Yarn website for installation instructions, documentation, and details about modern Yarn.
Conclusion
Yarn is a useful package manager for JavaScript development. It helps developers manage dependencies, maintain lockfiles, run scripts, and organize multiple packages through workspaces.
As you learn React, Vue, Node.js, Next.js, Vite, or other JavaScript technologies, understanding package managers such as Yarn and npm will help you work with real-world projects more confidently.
package.json and yarn.lock work together, and consult the official documentation for your installed version.
Related learning: