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.
What are we going to be covering Intro to npm workspaces Getting Started Using npm workspaces features Running scripts Installing new packages Managing workspaces properties Cutting releases of a workspace Executing package scripts What to be looking forward to
npm workspaces Name of the set of features to support managing nested packages from a single top-level package Centralizes installation of all dependencies to a single node_modules folder Uses only a single package-lock.json file What is it? ` ` ` `
npm workspaces Ideal for managing monorepos projects Single place to report & manage issues Single lint, build, test & release processes Eases coordination of changes across packages Eases development environment setup, troubleshooting Advantages
Real project Evolved sample project that is set up as a monorepo, containing multiple workspaces with different web services and dev tooling. Monorepo App ├── slides │ Workspace that contains the source │ for this slide presentation. ├──────── Slidev ├── user-sync service │ Workspace that has a service that │ listens to an endpoint and stores │ user data received to the db. ├──────── Fastify ├──────── Prisma ├──────── Tap (tests) ├── webapp service │ Workspace with a service that │ serves the UI to render the user list. ├──────── Next.js ├──────── Tailwind CSS ├──────── Prisma └──────── Jest (tests)
Installing packages Adding a dependency ( ) to a workspace named workspace-a : Something to be looking forward to: noHoist ` ` ` ` $ npm -w ./workspace-a install ` `
Running scripts By workspace name: It support scoped names too: By workspace path: The workspace argument can go anywhere: It’s possible to run scripts in the context of a workspace: $ npm -w workspace-a run dev $ npm -w @myscope/workspace-a run dev $ npm -w ./workspace-a run dev $ npm run dev -w ./workspace-a
Cutting releases of a workspace Something to be looking forward to: Auto commit and tagging Bumping package version for a workspace: $ npm version -w ./workspace-a
Executing package scripts Executing a package in the context of a workspace: Executing a package in the context of all configured workspaces: $ npm exec -w ./workspace-a $ npm exec --ws