Monorepo with npm Workspaces
Set up and manage a monorepo using npm workspaces for shared packages, consistent tooling, and streamlined development.
Prerequisites
- -Node.js 16 or later
- -npm 7 or later
Steps
Initialize the monorepo root
Create the root package.json with workspaces configuration pointing to your package directories.
Always set private: true on the root package.json to prevent accidentally publishing the monorepo itself.
Create workspace packages
Set up individual packages within the monorepo with their own package.json files.
Add cross-workspace dependencies
Reference shared packages as dependencies in other workspaces using the workspace protocol.
npm automatically symlinks workspace packages. Changes to shared code are immediately available without publishing.
Run scripts across workspaces
Execute npm scripts in specific workspaces or across all workspaces from the root.
Install dependencies to specific workspaces
Add dependencies to individual workspace packages rather than the root.
Install dependencies where they are used, not at the root. Root devDependencies should only be shared tooling like ESLint or TypeScript.
Create a shared TypeScript configuration
Set up a shared tsconfig that all workspace packages extend for consistent TypeScript settings.
Full Script
FAQ
Discussion
Loading comments...