workspaces | npm Docs (2024)

Working with workspaces

Select CLI Version:

Table of contents

Description

Workspaces is a generic term that refers to the set of features in the npm cli that provides support to managing multiple packages from your local files system from within a singular top-level, root package.

This set of features makes up for a much more streamlined workflow handling linked packages from the local file system. Automating the linking process as part of npm install and avoiding manually having to use npm link in order to add references to packages that should be symlinked into the current node_modules folder.

We also refer to these packages being auto-symlinked during npm install as a single workspace, meaning it's a nested package within the current local file system that is explicitly defined in the package.json workspaces configuration.

Defining workspaces

Workspaces are usually defined via the workspaces property of the package.json file, e.g:

{

"name": "my-workspaces-powered-project",

"workspaces": ["workspace-a"]

}

Given the above package.json example living at a current working directory . that contains a folder named workspace-a that itself contains a package.json inside it, defining a Node.js package, e.g:

.

+-- package.json

`-- workspace-a

`-- package.json

The expected result once running npm install in this current working directory . is that the folder workspace-a will get symlinked to the node_modules folder of the current working dir.

Below is a post npm install example, given that same previous example structure of files and folders:

.

+-- node_modules

| `-- workspace-a -> ../workspace-a

+-- package-lock.json

+-- package.json

`-- workspace-a

`-- package.json

Getting started with workspaces

You may automate the required steps to define a new workspace using npm init. For example in a project that already has a package.json defined you can run:

npm init -w ./packages/a

This command will create the missing folders and a new package.json file (if needed) while also making sure to properly configure the "workspaces" property of your root project package.json.

Adding dependencies to a workspace

It's possible to directly add/remove/update dependencies of your workspaces using the workspace config.

For example, assuming the following structure:

.

+-- package.json

`-- packages

+-- a

| `-- package.json

`-- b

`-- package.json

If you want to add a dependency named abbrev from the registry as a dependency of your workspace a, you may use the workspace config to tell the npm installer that package should be added as a dependency of the provided workspace:

npm install abbrev -w a

Note: other installing commands such as uninstall, ci, etc will also respect the provided workspace configuration.

Using workspaces

Given the specifities of how Node.js handles module resolution it's possible to consume any defined workspace by it's declared package.json name. Continuing from the example defined above, let's also create a Node.js script that will require the workspace-a example module, e.g:

// ./workspace-a/index.js

module.exports = 'a'

// ./lib/index.js

const moduleA = require('workspace-a')

console.log(moduleA) // -> a

When running it with:

node lib/index.js

This demonstrates how the nature of node_modules resolution allows for workspaces to enable a portable workflow for requiring each workspace in such a way that is also easy to publish these nested workspaces to be consumed elsewhere.

Running commands in the context of workspaces

You can use the workspace configuration option to run commands in the context of a configured workspace.

Following is a quick example on how to use the npm run command in the context of nested workspaces. For a project containing multiple workspaces, e.g:

.

+-- package.json

`-- packages

+-- a

| `-- package.json

`-- b

`-- package.json

By running a command using the workspace option, it's possible to run the given command in the context of that specific workspace. e.g:

npm run test --workspace=a

This will run the test script defined within the ./packages/a/package.json file.

Please note that you can also specify this argument multiple times in the command-line in order to target multiple workspaces, e.g:

npm run test --workspace=a --workspace=b

It's also possible to use the workspaces (plural) configuration option to enable the same behavior but running that command in the context of all configured workspaces. e.g:

npm run test --workspaces

Will run the test script in both ./packages/a and ./packages/b.

Commands will be run in each workspace in the order they appear in your package.json

{

"workspaces": [ "packages/a", "packages/b" ]

}

Order of run is different with:

{

"workspaces": [ "packages/b", "packages/a" ]

}

Ignoring missing scripts

It is not required for all of the workspaces to implement scripts run with the npm run command.

By running the command with the --if-present flag, npm will ignore workspaces missing target script.

npm run test --workspaces --if-present

See also

  • npm install
  • npm publish
  • npm run-script
  • config
Edit this page on GitHub

8 contributorsworkspaces | npm Docs (1)behnammodiworkspaces | npm Docs (2)Matsuuuworkspaces | npm Docs (3)relrelbworkspaces | npm Docs (4)lumaxisworkspaces | npm Docs (5)ruyadornoworkspaces | npm Docs (6)sethomasworkspaces | npm Docs (7)d-fischerworkspaces | npm Docs (8)ethomson

Last edited by behnammodi on August 31, 2021

workspaces | npm Docs (2024)

FAQs

Do you still need Lerna? ›

If you'd like to use pnpm and keep all of those features, you need extra tooling, but it doesn't necessarily need to be lerna. Both changesets and rush are good candidates for a replacement. If you're currently using lerna, or starting a new project, consider using other tools mentioned above.

What is the difference between npm and Pnpm workspace? ›

npm maintains a flattened dependency tree as of version 3. This leads to less disk space bloat, with a messy node_modules directory as a side effect. On the other hand, pnpm manages node_modules by using hard linking and symbolic linking to a global on-disk content-addressable store.

What do npm workspaces do? ›

With npm workspaces, you can share dependencies between packages. Instead of having multiple copies of the same dependency across different packages, npm workspaces will hoist common dependencies to the root of the monorepo, saving disk space and avoiding redundancy.

How to deploy npm workspace? ›

json` by adding a “workspaces” key with an array of workspace paths.
  1. { "name": "my-project", "version": "1.0.0", ...
  2. mkdir packages. cd packages. mkdir package1 package2 package3.
  3. # Install a dependency to the root workspace. npm install lodash. ...
  4. # Run a script across all packages. npm run build. ...
  5. # Publish all packages. npm publish.
Apr 3, 2024

What can I replace Lerna with? ›

What do I replace it with? ​ Replace lerna bootstrap with npm install (or yarn / pnpm ). If you are already performing your package manager's install command somewhere in your workflow before where you had previously called lerna bootstrap , then you can just delete it instead.

Is Lerna no longer maintained? ›

However, Lerna was considered obsolete or deprecated in April 2022 after an announcement was published in its main README file notifying users that it would no longer be maintained. Barely a month after this new development, Nrwl, the company that created Nx, a build system developed by ex-Googlers, took over Lerna.

Is yarn better than pnpm? ›

Improved speed​

The speed of package installation with pnpm is significantly better than npm and yarn. If you look at the below benchmark tests, you can see that pnpm performs better in most cases thano npm and yarn.

Which package manager is the fastest? ›

PNPM is the newest and fastest package manager. It works quite similarly to Yarn's PnP in that it also uses symlinks. What's pretty cool is that it doesn't download entire packages, only the differences between versions.

Is pnpm vs npm 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.

What is the purpose of workspaces? ›

A workspace is a technology framework that brings together all the applications, tools, and resources needed to get work done and collaborate securely, personalized for every individual in a digital world.

What are the disadvantages of npm workspaces? ›

One of the most common challenges developers face when using npm Workspaces is dealing with dependency conflicts. These conflicts often arise when different workspaces within the same monorepo rely on different versions of a particular package.

What the heck is npm? ›

npm can manage packages that are local dependencies of a particular project, as well as globally-installed JavaScript tools. When used as a dependency manager for a local project, npm can install, in one command, all the dependencies of a project through the package. json file.

How do I run npm on all workspaces? ›

Using workspaces. Most npm commands can now have workspace-related options added to make them run against just one (or all) of your workspaces. To run a command against a single workspace you can append --workspace=client . To run a command against all workspaces you can append --workspaces (note the s ).

What is pnpm workspace? ›

Pnpm workspace is a powerful tool for managing multiple packages in a single repository.

What is the difference between monorepo and workspace? ›

What are workspaces? ​ Workspaces are the name of individual packages that are part of the same project and that Yarn will install and link together to simplify cross-references. This pattern is often called monorepo when used in conjunction with a repository.

What can I use instead of Lerna version? ›

  • 5 Best Lerna Alternatives. Tools for JavaScript monorepos and multi-package repos to replace Lerna. ...
  • Bit. Bit is probably the first tool you should try out. ...
  • Yarn and pnpm workspaces. ...
  • NX. ...
  • Rush. ...
  • Turborepo.
May 4, 2022

What is the point of Lerna? ›

With Lerna, you can specify dependencies between different targets (npm scripts). So instead of managing dependencies and running them one by one, you can execute a single command to run all the dependent targets together. This saves you a lot of time, especially when you have a bunch of targets to handle.

Is Lerna NX better than Turborepo? ›

Turbo Repo: Turbo Repo enforces a specific project structure, which may be advantageous for consistency but might limit flexibility in project organization. Lerna: Lerna provides flexibility in project structure, allowing developers to organize packages in a way that suits their project's requirements.

What is the difference between Lerna and yarn workspace? ›

Lerna is used to optimize the management of monorepos. We'll use this tool to manage shared dependencies. Yarn Workspaces is used to optimize and link different packages together.

Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 5826

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.