NPM Workspaces for Monorepos
Intermediatev1.0.0
Set up npm workspaces to manage multiple packages in a single repository — shared dependencies, cross-package scripts, and publishing workflows for monorepo projects.
Content
Overview
NPM workspaces let you manage multiple packages in a single repository with shared dependencies, unified scripts, and cross-package references. No additional tooling like Lerna is required for most use cases.
Why This Matters
- -Shared dependencies — one node_modules, deduplicated packages
- -Cross-references — packages import each other without publishing
- -Unified commands — run scripts across all packages from the root
- -Atomic changes — modify multiple packages in a single commit
Step 1: Configure Root package.json
Step 2: Create Package Structure
Step 3: Cross-Package References
Step 4: Run Scripts Across Workspaces
Step 5: Root Scripts for Common Commands
Best Practices
- -Use scoped package names (
@myorg/package) for workspace packages - -Set
"private": trueon the root package.json - -Use
"*"version for internal workspace dependencies - -Run
npm installfrom the root — never from individual packages - -Use
--if-presentflag when not all packages have the script - -Keep shared dev dependencies (TypeScript, ESLint) at the root level
Common Mistakes
- -Running npm install inside individual workspace packages
- -Not using scoped names (name collisions with public packages)
- -Forgetting to set workspace packages as dependencies before importing
- -Publishing workspace packages without building first
FAQ
Discussion
Loading comments...