Levelling up Monorepos with npm Workspaces

Rate this content
Bookmark

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.

This talk has been presented at DevOps.js Conf 2022, check out the latest edition of this JavaScript Conference.

FAQ

The key advantages of using NPM workspaces include centralized dependency management, a single package lock file for all sub-packages, and streamlined task management across multiple packages in a monorepo setup. This results in a more efficient and manageable development process.

To start a new workspace in NPM, use the `npm init` command inside your project. This ensures that every required folder and package.json file is correctly set up and listed in the workspaces field of your top-level package.json.

The NPM pkg command is used to set or retrieve key-value pairs from the package.json files of your workspaces. It can be particularly powerful for managing configurations and dependencies across multiple workspaces efficiently.

NPM workspaces were first introduced in August 2020 with the release of NPM CLI version 7.0.

To add dependencies to a specific workspace, use the command `npm workspace <workspace_name> add <package_name>`. This command targets the specified workspace and installs the desired package directly into it, managing dependencies efficiently.

NPM workspaces are a set of features in NPM that allow for managing multiple nested packages within a single top-level package. This helps in centralizing the installation of dependencies into a single node_modules folder and managing a single package lock file, making it ideal for handling monorepos.

Yes, you can run scripts specifically for one workspace by using the command format `npm run --workspace=<workspace_name> <script_name>`. This allows for running tasks directly within the context of the specified workspace.

Both NPM and Yarn workspaces aim to solve similar problems related to managing monorepos. The main difference lies in their integration with their respective package managers. NPM workspaces are integrated with the NPM CLI, providing native support for managing node modules and dependencies in a unified manner.

Ruy Adorno
Ruy Adorno
33 min
25 Mar, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

NPM workspaces help manage multiple nested packages within a single top-level package, improving since the release of NPM CLI 7.0. You can easily add dependencies to workspaces and handle duplications. Running scripts and orchestration in a monorepo is made easier with NPM workspaces. The npm pkg command is useful for setting and retrieving keys and values from package.json files. NPM workspaces offer benefits compared to Lerna and future plans include better workspace linking and adding missing features.

1. Introduction to NPM Workspaces

Short description:

Hello, and welcome to leveling up monorepos with NPM workspaces. My name is Rui Aduno, a software developer in the NPM CLI team at GitHub. I have been working on NPM CLI for almost three years now. Let's start with an overview of NPM workspaces. Workspaces help you manage multiple nested packages within a single top-level package. It centralizes the installation of dependencies and allows you to run all tasks in a single place. We have been improving NPM workspaces since August 2020 with the release of NPM CLI 7.0. More improvements are coming.

Hello, and welcome to leveling up monorepos with NPM workspaces. Let me start by introducing myself. My name is Rui Aduno. I'm a software developer in the NPM CLI team at GitHub. I have been working on NPM CLI for almost three years now. I worked on the NPM v7 rewrite and a lot of those features that we're going to be talking about here today and especially in workspaces. I'm super excited to be sharing some of that work with you here today.

Let's start with an overview of what we're going to be covering here today. Let's start with an intro to what is in NPM workspaces and then cover some very practical examples trying to get as close to real-life usage as possible and also note some of the stuff that you should be looking forward to. So, let's get started.

NPM workspaces. What is it? So, let's start with workspaces, which is basically this concept introduced by Yarn a while ago and basically it is a way to help you manage many packages within a single repo and for NPM, NPM workspaces is kind of the broad name we gave, the set of features that help you achieve that, basically help you manage multiple nested package within a single top level package. So, it's going to help you centralize the installation of all the dependencies into a single node modules folder. So, you're going to end up also having only a single package lock file, and there are some advantages to that, it is definitely the ideal way for managing monorepos, and thanks to that you can have a single place to manage all your issues, and basically manage your project and your community. But also, into the technical side, you can also centralize, have a single place to run all your tasks, building, anything that you can kind of imagine, you kind of need everything together to get your project running with all the packages, you can have it all in a single place. So, it might help a lot depending on the style of the project you're trying to achieve. Another thing I would like to note here is the timeline, just to give a little bit of this notion of how we've been iteratively improving on NPM workspaces. And you can see it all started in August 2020 with the release of the 7.0 of the NPM CLI that first introduced support to installing NPM, the workspaces. And then in version 7.7, another big one, which kind of first introduced the configuration properties of a workspace or targeting all the workspaces and the first command that support them with NPM Run and NPM Exec. So there were many more, but I just kind of wanted to illustrate how we've been improving. And you can definitely wait to see more improvements coming on NPM Workspaces.

2. Adding Workspaces and Dependencies

Short description:

So let's get started with some examples here. I have this project that I have on the left-hand side here, that is a simple app that I put together trying to get as close as possible of a real-life project. This is kind of like a web app that consists of two different services. And with that I'm hoping to put some examples that are real close to how you would use this in your real life. Moving forward here, I'd like to highlight NPM Init. It's probably the best way of just starting your workspace. So I'm going to run a quick example here in my sample app. I'm going to create a new workspace. Let's say I'm going to call it the website. So I can run NPM init. And I'm targeting a website. So that's going to create a website folder with a package.json file inside it. And as I mentioned before, the other really important point that NPM init takes care of is placing the reference to the website inside my package.json file in my top level folder. So with that, it is all set up. If I npm install here, now the install tree is going to be tracking the website. And if I run npm ls again, I'm going to see website listed as one of the workspaces here highlighted in green. And we can move on to another very important aspect. How do you add dependencies to one of your workspaces? So a real quick example here again. I'm going to shift around the workspace configuration here, so you can see that you can practically go anywhere. So I can just declare an npm workspace, I'm going to target the website I just created.

So let's get started with some examples here. I have this project that I have on the left-hand side here, that is a simple app that I put together trying to get as close as possible of a real-life project. This is kind of like a web app that consists of two different services. So you can see I have my user sense service, my web app service, and I even configured a third workspace here, which is the slides that you're seeing right now. So they're all part of this monorepo app that I'm managing here.

And with that I'm hoping to put some examples that are real close to how you would use this in your real life. So you can see they all have a bunch of dependencies here using NextJS, the services using Fastify. So I can just quickly start by running NPM Ls here, and we can see how Ls is a command that is aware of workspaces, and it's going to highlight each of the workspaces in my project, and even list the dependencies of each workspace when I run NPM Ls. So it's the first command to know that has first class support for workspaces.

Moving forward here, I'd like to highlight NPM Init. It's probably the best way of just starting your workspace, because it's going to make sure every step that is needed is going to end up being there. So basically, everything you need to track a nested package as a workspace is make sure you have the folder with a package.json inside it, inside your project, and then just add that folder name to your workspaces field of your package.json in the top level. So using NPM init is going to make sure that those requirements are there. So I'm going to run a quick example here in my sample app. I'm going to create a new workspace. Let's say I'm going to call it the website. So I can run NPM init. I'm using dash y here to just accept all the defaults. And I'm targeting a website. So that's going to create a website folder with a package.json file inside it. It's going to print the contents of the package.json file over here. And as I mentioned before, the other really important point that NPM init takes care of is placing the reference to the website inside my package.json file in my top level folder. So with that, it is all set up. If I npm install here, now the install tree is going to be tracking the website. And if I run npm ls again, I'm going to see website listed as one of the workspaces here highlighted in green. So that is definitely the recommended way to get started with a new workspace inside your project.

And we can move on to another very important aspect. How do you add dependencies to one of your workspaces? So a real quick example here again. I'm going to shift around the workspace configuration here, so you can see that you can practically go anywhere. So I can just declare an npm workspace, I'm going to target the website I just created.

QnA