workspace

npm workspaces

A set of flags — not a standalone command — that let npm operate against one, several, or every workspace declared in the root package.json's `workspaces` array. Introduced in npm 7 and refined through npm 10.

npm <cmd> --workspace=<name> | --workspaces | -w <path>

Common flags

Flag Purpose
--workspace <name> (-w) Target a single workspace by name or path
--workspaces (-ws) Run the command in every workspace
--include-workspace-root Also include the root package in the run
--if-present Do not fail workspaces missing the script

Examples

npm install lodash -w apps/web

Add lodash only to the apps/web workspace

npm run build -ws --if-present

Build every workspace that has a build script

npm run test -w @acme/utils

Run tests in the workspace named @acme/utils

Gotcha

Workspaces share a single root node_modules and a single package-lock.json — do not run `npm install` inside a child workspace directory or you can desync the lockfile. Always operate from the repo root and target children with `-w`.

Related commands

← All npm commands · npm vs pnpm vs Yarn · Fix Node OOM