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
npm init
Creates a new package.json interactively, or when given an initializer (e.g. `npm init vite@latest`) delegates to `npm exec create-<initializer>`. `-y` accepts all defaults.
npm run
Executes a script from the `scripts` block of package.json in a shell where node_modules/.bin is on PATH. Alias: `run-script`; running `npm run` with no arg lists every available script.
npm install
Installs a package (and its dependencies), adding it to package.json and refreshing package-lock.json. Without arguments, installs everything already declared in the current project's package.json.