Yarn Modern with Plug’n’Play and "Zero-Installs" (2024)

Spencer Carney

Posted on

Yarn Modern with Plug’n’Play and "Zero-Installs" (3) Yarn Modern with Plug’n’Play and "Zero-Installs" (4) Yarn Modern with Plug’n’Play and "Zero-Installs" (5) Yarn Modern with Plug’n’Play and "Zero-Installs" (6) Yarn Modern with Plug’n’Play and "Zero-Installs" (7)

#yarn #plugnplay #javascript #tooling

Ever since Facebook released Yarn in 2016, it has become an essential tool for developers. According to Stack Overflow’s 2022 survey of 70,000 developers, 27% of those respondents use Yarn. Introducing a lock file and parallel downloads, Yarn pushed the envelope of what a JavaScript dependency management tool can do. The release of version 2 (known as Yarn Modern or Yarn Berry) introduced Plug'n'Play with Zero-Installs. In this article I will walk you through creating a project using the Yarn Modern, adding Plug'n'Play and Zero-Installs as we go.

What is Plug'n'Play and Zero-Installs?

Plug'n'Play is a strategy for installing a project’s dependencies. Zero-Installs is a collection of Yarn's features used to make your project as fast and stable as possible.

Have you ran into a problem with a project’s dependencies that was only solved with rm -rf node_modules? Or running a node script on your machine works but that same script fails to run on a coworker's? The Yarn docs calls this the “node_modules problem”.

The “node_modules problem” refers to the inefficiencies caused by using the Node Resolution Algorithm to look up a project’s dependencies. The Node Resolution Algorithm looks for files that are require’d or import’d, recursively (and indiscriminately) searching all the way to the home directory of the computer it is being run on. With the volume of files installed, and different stat and readdir calls, this traditional approach is costly in space and time. Every minute spent on CI / CD installing node modules and bootstrapping your application is money coming out of someone’s pocket.

Plug'n'Play and Zero-Installs provide a solution: replace large node_modules downloads with zipped cached files that unzip at runtime.

Setting up a new project with Yarn Modern

First, we need to upgrade to the latest version of Yarn.

npm i yarn -g

We are going to create a new folder and set up a git repo using Yarn inside of it.

mkdir yarn-moderncd yarn-moderngit inityarn init -y

Running ls -l should show us the following files:

We need to set up our project to install our dependencies in the node_modules folder. We can do that by running yarn config set nodeLinker: node-modules from the terminal. That shows us the following output:

➤ YN0000: Successfully set nodeLinker to 'node-modules'

Running that command creates a .yarnrc.yml file with the following contents:

nodeLinker: node-modules

We want to commit these files. Doing so will allow us to run git status to see subsequent changes in isolation. Before doing that, we need to make a change to .gitignore.

Let's look at the contents of .gitignore first:

.yarn/*!.yarn/patches!.yarn/plugins!.yarn/releases!.yarn/sdks!.yarn/versions# Swap the comments on the following lines if you don't wish to use Zero-Installs# Documentation here: https://yarnpkg.com/features/Zero-Installs!.yarn/cache#.pnp.*

There are a number of .yarn folders that we do not want to ignore. They can serve a purpose but they are beyond the scope of this article. Looking at the bottom section, there is the following comment:

Swap the comments on the following lines if you don't wish to use Zero-Installs

By following those instructions we will turn off Zero-Installs, which was set up for us when we ran yarn init -y. Turning it off now will help us better understand how Zero-Installs works when we enable it.

Update the bottom of the .gitignore to this:

#!.yarn/cache.pnp.*

Add node_modules to .gitignore as well.

When that is done, commit the changes.

Let's add lodash as a dependency.

yarn add lodash

Running git status we should see a familiar sight:

Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: package.json modified: yarn.lock

Running ls -l, we see a node_modules folder. We also have a .yarn folder. The .yarn folder has a cache folder along with a install-state.gz folder. We won't be covering what install-state.gz does but you can find out more about the files and folders that can end up in your .yarn folder here. .yarn/cache holds a local, cached copy of our project dependencies.

ls -ls .yarn/cache will show us something like this:

lodash-npm-4.17.21-6382451519-eb835a2e51.zip

If we were to delete node_modules and re-run yarn install, it would load it from .yarn/cache instead of fetching it from the remote repository. That is a nice touch.

Next we will create a small file in our project's root that loads lodash, index.js.

const _ = require('lodash');console.log(_.camelCase('FOO_BAR_BAZ'));

Running node index.js we get the following:

fooBarBaz

As expected, we are loading lodash from node_modules. Commit this change. Next we are going to enable Plug'n'Play.

Adding Plug'n'Play

To add Plug'n'Play, we need to change the nodeLinker config value. Run the following command in your terminal:

yarn config set nodeLinker pnp

Done successfully, that will output the following:

➤ YN0000: Successfully set nodeLinker to 'pnp'

You can also change this setting manually by opening .yarnrc.yml and changing nodeLinker to pnp:

nodeLinker: pnp

Run yarn install. You should see the following line somewhere in the output:

➤ YN0031: │ One or more node_modules have been detected and will be removed. This operation may take some time.

Running ls -l should show that there is no longer a node_modules directory. Lets make sure our index.js file is still working:

node index.js

Uh oh! We got an error, that looks something like this:

node:internal/modules/cjs/loader:942 throw err; ^Error: Cannot find module 'lodash'Require Stack:...

Using Plug'n'Play requires using the yarn binary, like so:

yarn node index.js

It works! Running yarn node index.js, we can see console.log from our index.js file. Instead of node_modules, our dependencies are loading from .yarn/cache instead.

Zero-Installs

To achieve zero-install state with Plug'n'Play, we need make an update to .gitignore, replacing its contents with the following:

.yarn/*!.yarn/cache!.yarn/patches!.yarn/plugins!.yarn/releases!.yarn/sdks!.yarn/versions

Running git status, you see the previously ignored Yarn cache files along with a .pnp.cjs showing up as untracked files. We will commit these files to the repo. This how we achieve Zero-Installs, by adding our .yarn/cache files to the repo along with the .pnp.cjs file. Yarn's website has a good description of what the .pnp.cjs file is used for:

The .pnp.cjs file contains various maps: one linking package names and versions to their location on the disk and another one linking package names and versions to their list of dependencies. With these lookup tables, Yarn can instantly tell Node where to find any package it needs to access, as long as they are part of the dependency tree, and as long as this file is loaded within your environment.

Adding your dependencies to your repo may feel strange at first but you quickly get used to it.

If you are adding Plug'n'Play to a new project, you may run into some challenges. Plug'n'Play is more strict compared to other JavaScript package managers. This is by design. 3rd party packages may not have listed their dependencies in a manner that works with the strict nature of Plug'n'Play. This is either a feature or bug of how other package managers work. It all depends on how you look at it. Yarn Modern provides tools to tackle these challenges: yarn patch, yarn patch-commit, and packageDependencies. There is also some additional work needed to get your IDE to work with Plug'n'Play. I will cover these topics in a future post.

Yarn with Plug'n'Play and Zero-Installs is an exciting addition to the package manager landscape. Skipping the need to install node modules on every build results in faster deployments. Getting the latest dependencies is as simple as running git pull. While it may seem unconventional at first, once adopted, it can increase a team's overall productivity.

Yarn Modern with Plug’n’Play and "Zero-Installs" (2024)

FAQs

What are the downsides of pnpm? ›

PNPM has the same drawback as PnP - namely that it may not support all packages. The node_modules structure of PNPM is easy to read and nicely done - what's a package, what's a link (see image above), etc, is all quite clear.

What is zero installs yarn? ›

Zero-installs are the combination of two Yarn features that allow you to skip having to think about running yarn install when switching branches - a requirement otherwise easy to forget until you see your tools crash.

Is yarn still worth it? ›

If speed and deterministic dependencies matter to you, Yarn will be your best bet. Yarn popularised lock files, ensuring that the same versions of dependencies are installed across different systems, and was originally built to be faster, which it still is today.

What is the difference between yarn add and yarn install? ›

yarn add : adds a package to use in your current package. yarn init : initializes the development of a package. yarn install : installs all the dependencies defined in a package.

Which is better, Yarn or pnpm? ›

PNPM stands out in terms of disk space efficiency by using a global store for all packages and linking them to the projects. Yarn also tries to be efficient by installing packages in a flat manner, but it can lead to version conflicts.

Why is pnpm so slow? ›

maybe its due to your disk read write speeds.. because pnpm copies half of the files/folders from . pnpm-store So if you're on a HDD it'll be really slow as compared to being on SSDs.

What is the difference between zero install and PNP? ›

Plug'n'Play is a strategy for installing a project's dependencies. Zero-Installs is a collection of Yarn's features used to make your project as fast and stable as possible.

Is yarn deprecated? ›

Yarn Classic (v1) entered maintenance mode in January 2020 and will eventually reach end-of-life. It is highly recommended to Migrate to the latest version. Yarn Classic only receives critical and security fixes.

What is the best way to install yarn? ›

Install via npm

It is recommended to install Yarn through the npm package manager, which comes bundled with Node.js when you install it on your system.

Is Bun faster than PNPM? ›

When writing files from the cache into node_modules , Bun uses the fastest system calls available on your operating system. This is dramatically faster than naively symlinking from a global cache like pnpm .

What is yarn outdated? ›

Lists version information for all package dependencies. This information includes the currently installed version, the desired version based on semver, and the latest available version.

What is PNPM in 2024? ›

In 2024, PNPM, NPM, and Yarn continue to evolve. PNPM focuses on speed and efficiency, NPM remains committed to enhancing security and performance, and Yarn pushes the boundaries of innovation with updates to Plug'n'Play (PnP) and workspace management. Keep an eye out for the following improvements.

When should I run yarn install? ›

yarn install is used to install all dependencies for a project. This is most commonly used when you have just checked out code for a project, or when another developer on the project has added a new dependency that you need to pick up.

Is yarn install faster than npm? ›

Yarn is generally faster than NPM due to parallel installation and caching mechanisms. NPM is often slower than Yarn, especially in large projects with many dependencies. Yarn ensures deterministic builds with the lockfile, which specifies exact versions of dependencies.

How do I know where my yarn is installed? ›

yarn/bin . yarn global dir will print the output of the global installation folder that houses the global node_modules . By default that will be: ~/. config/yarn/global .

Is pnpm better than npm? ›

​ pnpm is a drop-in replacement for npm. It is built on top of npm and is much faster and more efficient than its predecessor. It is highly disk efficient and solves inherent issues in npm.

Should I switch to pnpm? ›

PNPM: PNPM is 3 times faster and more efficient than NPM. With both cold and hot cache, PNPM is faster than Yarn. 👉 Pnpm simply links files from the global store, while yarn copies files from its cache. Package versions are never saved more than once on a disk.

What are the cons of constructor injection? ›

Cons of Constructor Injection:
  • Boilerplate Code: Constructor Injection might result in more boilerplate code, especially in classes with many dependencies.
  • Modifiability: Adding or removing dependencies may require changes to the constructor and potentially multiple places in the code.
Jan 1, 2024

What is the advantage of pnpm? ›

PNPM: Utilizes a shared dependency mechanism that allows different projects to use the same copy of a package. This efficient approach minimizes duplication and reduces disk space usage.

Top Articles
CPFB | CPF interest rates from 1 April 2024 to 30 June 2024
Passive Income in Defi—A Traditional Investor’s Guide to Earning Yield in Crypto
Poe T4 Aisling
Access-A-Ride – ACCESS NYC
Chambersburg star athlete JJ Kelly makes his college decision, and he’s going DI
Www.politicser.com Pepperboy News
Lighthouse Diner Taylorsville Menu
Chalupp's Pizza Taos Menu
Needle Nose Peterbilt For Sale Craigslist
Craigslist/Phx
Wunderground Huntington Beach
Hmr Properties
Amelia Bissoon Wedding
Superhot Unblocked Games
Funny Marco Birth Chart
Radio Aleluya Dialogo Pastoral
The most iconic acting lineages in cinema history
Craigslist Farm And Garden Tallahassee Florida
Dignity Nfuse
Roll Out Gutter Extensions Lowe's
Kountry Pumpkin 29
Uta Kinesiology Advising
Icivics The Electoral Process Answer Key
Samantha Aufderheide
Dr Ayad Alsaadi
Dcf Training Number
Yosemite Sam Hood Ornament
Dark Entreaty Ffxiv
Colonial Executive Park - CRE Consultants
Jesus Revolution Showtimes Near Regal Stonecrest
Temu Seat Covers
Evil Dead Rise Ending Explained
Cosas Aesthetic Para Decorar Tu Cuarto Para Imprimir
Yayo - RimWorld Wiki
Gopher Hockey Forum
In hunt for cartel hitmen, Texas Ranger's biggest obstacle may be the border itself (2024)
Hoofdletters voor God in de NBV21 - Bijbelblog
Sports Clips Flowood Ms
Obsidian Guard's Skullsplitter
The 38 Best Restaurants in Montreal
Dollar Tree's 1,000 store closure tells the perils of poor acquisitions
Infinite Campus Farmingdale
What Is A K 56 Pink Pill?
Weekly Math Review Q2 7 Answer Key
Tgirls Philly
Hkx File Compatibility Check Skyrim/Sse
Rs3 Nature Spirit Quick Guide
BCLJ July 19 2019 HTML Shawn Day Andrea Day Butler Pa Divorce
705 Us 74 Bus Rockingham Nc
Streameast Io Soccer
Latest Posts
Article information

Author: Golda Nolan II

Last Updated:

Views: 6440

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Golda Nolan II

Birthday: 1998-05-14

Address: Suite 369 9754 Roberts Pines, West Benitaburgh, NM 69180-7958

Phone: +522993866487

Job: Sales Executive

Hobby: Worldbuilding, Shopping, Quilting, Cooking, Homebrewing, Leather crafting, Pet

Introduction: My name is Golda Nolan II, I am a thoughtful, clever, cute, jolly, brave, powerful, splendid person who loves writing and wants to share my knowledge and understanding with you.