Package Management in Monorepos

Rate this content
Bookmark

We’ll talk about some of the pain points and look into recipes for effective package management in monorepos. 
We’ll discuss how package management works with npm, pnpm, and Yarn. Furthermore, I’ll show you a new tool that is less known but improves developer experience by a lot.

Zoltan Kochan
Zoltan Kochan
19 min
15 Feb, 2024

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This Talk discusses pain points and effective package management in monorepos, including the use of hoisted or isolated layouts and the challenges of working with peer dependencies. It introduces the tool Bit, which addresses these issues and handles dependency management and version control. Bit enables automatic installation and management of dependencies, supports multiple versions of a peer dependency, and seamlessly updates components across different environments.

Available in Español: Gestión de paquetes en Monorepos

1. Introduction to Package Management in Monorepos

Short description:

My name is Zoltan Koçan. I'll discuss the pain points and share recipes for effective package management in monorepos. I handle dependency management at Bit and I'm the lead maintainer of the pnpm project. The history of monorepo tooling for JS projects starts with Babel and Lerna. By 2017, pnpm, Yarn, and npm all shipped monorepo support. Package managers can arrange dependencies in hoisted or isolated layouts, with pnpm using the isolated node modules layout.

My name is Zoltan Koçan. In my presentation, I want to talk about package management in monorepos. I'll discuss some of the pain points and share recipes for effective package management in monorepos.

Currently, I work at Bit, where I handle dependency management related tasks. I'm also the lead maintainer of the pnpm open-source project, which is a JS package manager. Before Bit, I worked at JustAnswer. At JustAnswer we had a huge monorepo with hundreds of components. Installation with npm took 30 minutes in that monorepo. That was the main reason I started contributing to a faster alternative, pnpm. With pnpm, we were able to reduce installation time to about 90 seconds.

Let's briefly talk about the history of monorepo tooling for JS projects. Babel was one of the most influential projects in the JavaScript ecosystem, and it was probably one of the first popular open-source JS projects that used a monorepository. The creators of Babel have created Lerna in 2015. Lerna was able to install dependencies in a monorepo using npm-cli under the hood. With that said, installation with Lerna was terribly slow, to say the least. Everyone knew that package managers should implement installation in monorepos out of the box. By 2017, both pnpm and Yarn have shipped monorepo support. Yarn has called this feature workspaces installation, while pnpm has used the singular term workspace. In a couple of years, npm had also shipped workspaces support. As of today, there are three popular mature Node.js package managers with built-in monorepo support.

There are two ways package managers can arrange dependencies in a monorepo, hoisted and isolated. All three package managers support both layouts. By default, Yarn and npm use a hoisted approach. With this approach, all direct and indirect dependencies are placed in the roots non-modules directory. If there are multiple versions of the same dependencies, one of the versions gets nested. As you can see on this slide, there are two different versions of lodash. So one of the versions is hoisted to the root of the monorepo, while the other one is nested inside app2. pnpm uses a different layout called isolated node modules. With the isolated node modules, the dependencies of every package are nested. The benefit of this approach is that packages only have access to their own dependencies.

2. Dependency Management in Monorepos

Short description:

While with hoisted layout, all projects would have access to the cookie package. It's really easy to mess up dependencies in a monorepo. Main.js in app1 is using lodash listed in the dev.dependencies. Working with peer dependencies in monorepos is challenging. It is crucial to use a single version of the peer dependency across all the Workspace packages. Only Yarn currently supports syncing versions of dependencies out of the box. pnpm has plans to introduce this feature through Workspace catalogs. pnpm offers a feature to support multiple versions of a peer dependency known as injected dependencies.

While with hoisted layout, all projects would have access to the cookie package, with an isolated node modules layout, projects have access only to their own dependencies. So in this case, only app1 will be able to require cookie.

I think most people agree that monorepos provide a superior developer experience. Despite this, it's really easy to mess up dependencies in a monorepo. As you can see in this example, the app is using a cookie but doesn't list cookie in its dependencies. This code will work locally because cookie is found in the node modules directory of a parent directory. However, it will break when someone installs app1 outside of the monorepo.

Another issue in this example is that main.js in app1 is using lodash. Main.js is production code, but lodash is listed in the dev.dependencies. It means that this code will work locally, but it will break in production where dev.dependencies are not installed. To catch these two specific issues, you may use a special rule in eslint, the noextraneous dependencies rule from the import plugin. If you configure this linting rule, eslint will notify you of dependencies that are imported but not declared in package.json. In this example, you will get an error about cookie being used in app1. eslint will also notify you about lodash being a dev.dependency. You avoid it if it is used by production code.

Working with peer dependencies in monorepos is challenging. It is crucial for the peer dependencies to be singletons during runtime. If possible, you should try to use a single version of the peer dependency across all the Workspace packages. As you can see in this example, both card and button reference the same react version. This will work fine. Whether you are dealing with peer dependencies or not, it is preferable to use the same version of a dependency across all of your projects. Doing so can help you avoid issues related to peer dependencies and reduce the size of your packages. To the best of my knowledge, only Yarn currently supports syncing versions of dependencies out of the box using constraints. pnpm has plans to introduce this feature through Workspace catalogs. It is also possible to use third-party tools for finding version duplicates. Multiple third-party tools act as linters to verify version inconsistency. One such tool is Syncpack. On large monorepos, it can sometimes become challenging to avoid having multiple versions of a peer dependency. Among npm, yarn, and pnpm, only pnpm offers a feature to support multiple versions of a peer dependency. This feature is known as injected dependencies.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Levelling up Monorepos with npm Workspaces
DevOps.js Conf 2022DevOps.js Conf 2022
33 min
Levelling up Monorepos with npm Workspaces
Top Content
Learn more about how to leverage the default features of npm workspaces to help you manage your monorepo project while also checking out some of the new npm cli features.
pnpm – a Fast, Disk Space Efficient Package Manager for JavaScript
DevOps.js Conf 2022DevOps.js Conf 2022
31 min
pnpm – a Fast, Disk Space Efficient Package Manager for JavaScript
You will learn about one of the most popular package managers for JavaScript and its advantages over npm and Yarn.A brief history of JavaScript package managersThe isolated node_modules structure created pnpmWhat makes pnpm so fastWhat makes pnpm disk space efficientMonorepo supportManaging Node.js versions with pnpm
The Zen of Yarn
DevOps.js Conf 2022DevOps.js Conf 2022
31 min
The Zen of Yarn
In the past years Yarn took a spot as one of the most common tools used to develop JavaScript projects, in no small part thanks to an opinionated set of guiding principles. But what are they? How do they apply to Yarn in practice? And just as important: how do they benefit you and your projects?
In this talk we won't dive into benchmarks or feature sets: instead, you'll learn how we approach Yarn’s development, how we explore new paths, how we keep our codebase healthy, and generally why we think Yarn will remain firmly set in our ecosystem for the years to come.
End the Pain: Rethinking CI for Large Monorepos
DevOps.js Conf 2024DevOps.js Conf 2024
25 min
End the Pain: Rethinking CI for Large Monorepos
Scaling large codebases, especially monorepos, can be a nightmare on Continuous Integration (CI) systems. The current landscape of CI tools leans towards being machine-oriented, low-level, and demanding in terms of maintenance. What's worse, they're often disassociated from the developer's actual needs and workflow.Why is CI a stumbling block? Because current CI systems are jacks-of-all-trades, with no specific understanding of your codebase. They can't take advantage of the context they operate in to offer optimizations.In this talk, we'll explore the future of CI, designed specifically for large codebases and monorepos. Imagine a CI system that understands the structure of your workspace, dynamically parallelizes tasks across machines using historical data, and does all of this with a minimal, high-level configuration. Let's rethink CI, making it smarter, more efficient, and aligned with developer needs.
Yarn 4 - Modern Package Management
JSNation 2022JSNation 2022
28 min
Yarn 4 - Modern Package Management
Top Content
Yarn 4 is the next major release of your favourite JavaScript package manager, with a focus on performance, security, and developer experience. All through this talk we'll go over its new features, major changes, and share our long-term plans for the project.If you only heard about Yarn without trying it yet, if you're not sure why people make such a fuss over package managers, if you wonder how your package manager can make your work simpler and safer, this is the perfect talk for you!
Federated Microfrontends at Scale
React Summit 2023React Summit 2023
31 min
Federated Microfrontends at Scale
Top Content
The talk will be a story of how Personio went from rendering through a Monolithical PHP architecture, to a microfrontend oriented Next JS app, powered by Module Federation and the NX monorepo toolchain.

Workshops on related topic

React at Scale with Nx
React Summit 2023React Summit 2023
145 min
React at Scale with Nx
Top Content
Featured WorkshopFree
Isaac Mann
Isaac Mann
We're going to be using Nx and some its plugins to accelerate the development of this app.
Some of the things you'll learn:- Generating a pristine Nx workspace- Generating frontend React apps and backend APIs inside your workspace, with pre-configured proxies- Creating shared libs for re-using code- Generating new routed components with all the routes pre-configured by Nx and ready to go- How to organize code in a monorepo- Easily move libs around your folder structure- Creating Storybook stories and e2e Cypress tests for your components
Table of contents: - Lab 1 - Generate an empty workspace- Lab 2 - Generate a React app- Lab 3 - Executors- Lab 3.1 - Migrations- Lab 4 - Generate a component lib- Lab 5 - Generate a utility lib- Lab 6 - Generate a route lib- Lab 7 - Add an Express API- Lab 8 - Displaying a full game in the routed game-detail component- Lab 9 - Generate a type lib that the API and frontend can share- Lab 10 - Generate Storybook stories for the shared ui component- Lab 11 - E2E test the shared component
Node Monorepos with Nx
Node Congress 2023Node Congress 2023
160 min
Node Monorepos with Nx
Top Content
WorkshopFree
Isaac Mann
Isaac Mann
Multiple apis and multiple teams all in the same repository can cause a lot of headaches, but Nx has you covered. Learn to share code, maintain configuration files and coordinate changes in a monorepo that can scale as large as your organisation does. Nx allows you to bring structure to a repository with hundreds of contributors and eliminates the CI slowdowns that typically occur as the codebase grows.
Table of contents:- Lab 1 - Generate an empty workspace- Lab 2 - Generate a node api- Lab 3 - Executors- Lab 4 - Migrations- Lab 5 - Generate an auth library- Lab 6 - Generate a database library- Lab 7 - Add a node cli- Lab 8 - Module boundaries- Lab 9 - Plugins and Generators - Intro- Lab 10 - Plugins and Generators - Modifying files- Lab 11 - Setting up CI- Lab 12 - Distributed caching
Finding, Hacking and fixing your NodeJS Vulnerabilities with Snyk
JSNation 2022JSNation 2022
99 min
Finding, Hacking and fixing your NodeJS Vulnerabilities with Snyk
WorkshopFree
Matthew Salmon
Matthew Salmon
npm and security, how much do you know about your dependencies?Hack-along, live hacking of a vulnerable Node app https://github.com/snyk-labs/nodejs-goof, Vulnerabilities from both Open source and written code. Encouraged to download the application and hack along with us.Fixing the issues and an introduction to Snyk with a demo.Open questions.
Build Web3 apps with React
React Summit 2022React Summit 2022
51 min
Build Web3 apps with React
WorkshopFree
Shain Dholakiya
Shain Dholakiya
The workshop is designed to help Web2 developers start building for Web3 using the Hyperverse. The Hyperverse is an open marketplace of community-built, audited, easy to discover smart modules. Our goal - to make it easy for React developers to build Web3 apps without writing a single line of smart contract code. Think “npm for smart contracts.”
Learn more about the Hyperverse here.
We will go over all the blockchain/crypto basics you need to know to start building on the Hyperverse, so you do not need to have any previous knowledge about the Web3 space. You just need to have React experience.