npm workspaces Explained

What npm workspaces does, with an example.

Published November 12, 2026

npm workspaces let one repository contain several packages that share a single node_modules and lockfile, with local packages linked automatically. They are npm's built-in monorepo support.

How it works

Declare workspaces in the root package.json. Packages can depend on each other by name and are symlinked.

Run scripts in one or all workspaces with -w or --workspaces.

Try it

npm install
(installs all workspace dependencies at the root)
npm run build -w packages/ui

Practical examples

Root package.json

{ "name": "mono", "private": true, "workspaces": ["apps/*", "packages/*"] }

Run a script in one workspace

npm run build -w packages/ui

Run in all

npm run test --workspaces --if-present

Add a dependency to a workspace

npm install lodash -w apps/web

Common mistakes

  • Forgetting private: true on the root.
  • Version mismatches between workspaces.
  • Hoisting surprises with duplicated dependencies.

Key takeaways

  • npm workspaces let one repository contain several packages that share a single node_modules and lockfile, with local packages linked automatically.
  • Run npm help <command> for every option.
  • Commit package-lock.json.

FAQ

npm workspaces vs Turborepo/Nx?

Workspaces manage dependencies; Turborepo/Nx add task orchestration and caching.

Does this work with yarn or pnpm?

They have equivalents with similar commands.

More JavaScript articles