Managing Multiple Packages with npm Workspaces: A Complete Guide

Mar 17, 2025

Managing Multiple Packages with npm Workspaces: A Complete Guide

Learn how to manage multiple packages in a monorepo using npm Workspaces. Simplify development, avoid version conflicts, and streamline dependencies efficiently.

npm Workspaces is a powerful tool that allows developers to manage multiple packages in a single repository. With npm Workspaces, developers can maintain interdependent packages while ensuring their versioning is kept in sync. This blog will explain the features and APIs of npm Workspaces, as well as how to use them effectively.

What are npm Workspaces?

npm Workspaces is a feature of the Node.js package manager (npm) that allows you to group multiple packages in a single repository. When you use npm Workspaces, each package has its own node_modules folder, but dependencies that are shared between packages are hoisted to the root node_modules folder. This means that when a package depends on a shared dependency, it can use the version that is already installed instead of installing its copy.

Features of npm Workspaces

  1. Shared Dependencies: With npm Workspaces, dependencies that are shared between packages are hoisted to the root node_modules folder. This reduces duplication and ensures that all packages use the same version of the dependency.
  2. Version Synchronization: npm Workspaces ensures that all packages in the workspace use the same version of a given dependency. This avoids version conflicts and makes it easier to maintain the codebase.
  3. Simplified Development Workflow: npm Workspaces makes it easy to work on multiple packages simultaneously. Developers can run commands across all packages at once, such as installing dependencies or running tests.
  4. Monorepo Support: npm Workspaces is ideal for monorepo architectures, where multiple packages are managed in a single repository.

APIs of npm Workspaces

npm Workspaces provides several APIs that developers can use to interact with the workspace.

  1. npm install: Developers can use the npm install command to install dependencies for all packages in the workspace.
  2. npm run: Developers can use the npm run command to run scripts across all packages in the workspace. For example, npm run build will run the build script in each package.
  3. npm link: Developers can use the npm link command to create a symbolic link between packages in the workspace. This makes it easy to test changes to one package in the context of another package.
  4. npm publish: Developers can use the npm publish command to publish all packages in the workspace to the npm registry.

How to Use npm Workspaces

Here are the steps to set up npm Workspaces:

  1. Create a new directory for your workspace.
  2. Initialize the directory as an npm workspace by running npm init -w.
  3. Create subdirectories for each package in the workspace.
  4. Initialize each package with npm init.
  5. Add dependencies to each package as needed.
  6. In the root package.json file, add a workspaces key with an array of directories that contain packages. For example:
{
  "name": "my-workspace",
  "private": true,
  "workspaces": [
    "packages/*"
  ]
}
  1. Run npm install to install dependencies for all packages in the workspace.
  2. Use the npm run command to run scripts across all packages in the workspace.

Example 1: Web app with Shared Components

Suppose you are building a web application with multiple components, and you want to share some code between the components. With npm Workspaces, you can create a workspace with one package for each component and another package for the shared code. Here's what the directory structure might look like:

my-web-app/
├── package.json
├── packages/
│   ├── component1/
│   ├── component2/
│   └── shared/

In the package.json file at the root of the workspace, you can specify the workspaces as follows:

{
  "name": "my-web-app",
  "private": true,
  "workspaces": [
    "packages/*"
  ]
}

Now, when you run npm install in the root directory, npm will install the dependencies for all packages in the workspace. If a package depends on a shared dependency, npm will hoist that dependency to the root node_modules folder.

Example 2: Monorepo with Shared Utility Functions

my-monorepo/
├── package.json
├── packages/
│   ├── project1/
│   ├── project2/
│   └── utils/

In the package.json file at the root of the workspace, you can specify the workspaces as follows:

{
  "name": "my-monorepo",
  "private": true,
  "workspaces": [
    "packages/*"
  ]
}

Now, when you run npm install in the root directory, npm will install the dependencies for all packages in the workspace. If a package depends on a shared dependency, npm will hoist that dependency to the root node_modules folder. Similarly, if a package depends on a utility function in the utils package, npm will install that package in the root node_modules folder.

Conclusion

As these examples demonstrate, npm Workspaces can be a powerful tool for managing multiple packages in a single repository. By reducing duplication and ensuring version synchronization, npm Workspaces can help you avoid version conflicts and maintain a clean codebase. Whether you're working on a monorepo or a web application with shared components, npm Workspaces is worth considering.

Subscribe to Our Newsletter

More from the engineering frontline.

Dive deep into our research and insights on design, development, and the impact of various trends to businesses.
Insight
Why Legacy Systems Make Business Growth More Expensive: Navigate A Smarter Path to Legacy Modernization
Sep 23, 2026

Why Legacy Systems Make Business Growth More Expensive: Navigate A Smarter Path to Legacy Modernization

Learn how legacy systems make business growth more expensive and how edge-first modernization can remove constraints without replacing the existing system.

Insight
SSO, Audit Logs and RBAC: The Enterprise Features AI Prototyping Tools Do Not Cover | Sarika Gautam
Sep 23, 2026

SSO, Audit Logs and RBAC: The Enterprise Features AI Prototyping Tools Do Not Cover | Sarika Gautam

Why AI-generated prototypes fail enterprise review: the context behind SSO, the cost of skipping audit logs, and how role explosion makes RBAC a product of its own.

Insight
Feature Flags as Technical Debt: The Cleanup Nobody Schedules
Sep 21, 2026

Feature Flags as Technical Debt: The Cleanup Nobody Schedules

This blog explains how unmanaged feature flags create technical debt and how teams can detect, manage, and remove them safely.

Insight
Building AI-First Enterprises: Why System Design Matters More Than AI Adoption
Sep 21, 2026

Building AI-First Enterprises: Why System Design Matters More Than AI Adoption

This blog explores how system design, architecture, and validation shape AI-first enterprises, while examining AI’s impact on software engineering and human decision-making.

Insight
The Product Studio in the AI Era: What Actually Changes | Sarika Gautam
Sep 21, 2026

The Product Studio in the AI Era: What Actually Changes | Sarika Gautam

What changes in product development when AI writes the code: the shift to architecture, the token cost of unplanned builds, and why juniors still matter.

Insight
AI and the Future of Digital Customer Experience: Where Technology Meets Human Creativity
Sep 18, 2026

AI and the Future of Digital Customer Experience: Where Technology Meets Human Creativity

A discussion on how AI, human creativity, research, and cross-functional collaboration are shaping the future of digital customer experience.

Insight
Scaling Down Before Scaling Up: Why Bigger Servers Don’t Fix Bad Architecture
Sep 17, 2026

Scaling Down Before Scaling Up: Why Bigger Servers Don’t Fix Bad Architecture

This blog explores how inefficient backend architecture can cause performance issues even under low traffic, covering practical ways to reduce database load, API latency, and resource usage before scaling infrastructure.

The Right Conversation Can

Save You Six Months.

Book a call