www.LinuxHowtos.org
WORKSPACES
Section: (7)Updated: October 2025
Index Return to Main Contents
NAME
workspaces- Working with workspacesDescription
Workspaces is a generic term that refers to the set of features in the npm cli that provides support for managing multiple packages from your local file system from within a singular to-level, root package. This set of features makes up for a much more streamlined workflow handling linked packages from the local file system. It automates the linking process as part of npm install and removes the need to manually 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 aut-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 [rs]fBpackage.json[rs]fR la/configurin-npm/packag-json#workspacesra workspaces configuration.Defining workspaces
Workspaces are usually defined via the workspaces property of the [rs]fBpackage.json[rs]fR la/configurin-npm/packag-json#workspacesra file, e.g:-
{ "name": "m-workspace-powere-project", "workspaces": [lB] "packages/a" [rB] }
-
- package.json - packages - a | - package.json
-
- node_modules | - a-> ../packages/a - packag-lock.json - package.json - packages - a | - package.json
Getting started with workspaces
You may automate the required steps to define a new workspace using npm help init. For example in a project that already has a package.json defined you can run:-
npm init-w ./packages/a
Adding dependencies to a workspace
It's possible to directly add/remove/update dependencies of your workspaces using the [rs]fBworkspace[rs]fR config la/usin-npm/config#workspacera. For example, assuming the following structure:-
- package.json - packages - a | - package.json - b - package.json
-
npm install abbrev-w a
Using workspaces
Given the specifics of how Node.js handles module resolution lahttps://nodejs.org/dist/lates-v14.x/docs/api/modules.html#modules_all_togetherra it's possible to consume any defined workspace by its 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:-
// ./packages/a/index.js module.exports = 'a' // ./lib/index.js const moduleA = require('a') console.log(moduleA) //-> a
Running commands in the context of workspaces
You can use the workspace configuration option to run commands in the context of a configured workspace. Additionally, if your current directory is in a workspace, the workspace configuration is implicitly set, and prefix is set to the root 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
-
npm run test-workspace=a
-
cd packages/a && npm run test
-
npm run test-workspace=a-workspace=b
-
npm run test-workspace=packages
-
npm run test-workspaces
-
{ "workspaces": [lB] "packages/a", "packages/b" [rB] }
-
{ "workspaces": [lB] "packages/b", "packages/a" [rB] }
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 -i-present flag, npm will ignore workspaces missing target script.-
npm run test-workspaces-i-present
See also
-
- *
- npm help install
- *
- npm help publish
- *
- npm help run
- *
- npm help config