DEV Community

Cover image for Nx Monorepo: A Practical Guide to Building Scalable JavaScript and TypeScript Architectures
Abanoub Kerols
Abanoub Kerols

Posted on

Nx Monorepo: A Practical Guide to Building Scalable JavaScript and TypeScript Architectures


Modern applications rarely remain small.

A project may start with a single frontend application and a simple backend. Over time, it can grow into multiple applications, shared UI components, authentication libraries, API clients, utilities, domain logic, and even multiple backend services.

At that point, teams usually face an important architectural question:

Should we keep everything in one repository, or split the system into multiple repositories?

This is where Monorepos and tools like Nx become extremely useful.

In this article, we'll explore Nx from both a practical and architectural perspective.

We'll cover:

  • What a monorepo is
  • Monorepo vs polyrepo
  • What Nx actually does
  • Nx workspace architecture
  • Applications vs libraries
  • Project boundaries
  • Dependency graphs
  • Affected commands
  • Task caching
  • Generators
  • Code sharing
  • Angular + Node.js monorepos
  • CI/CD optimization
  • Common mistakes
  • A practical production-style example

1. What Is a Monorepo?

A monorepo is a repository that contains multiple applications and/or libraries.

For example:

company-platform/
│
├── apps/
│   ├── web/
│   ├── admin/
│   ├── api/
│   └── worker/
│
├── libs/
│   ├── auth/
│   ├── ui/
│   ├── database/
│   ├── api-client/
│   └── shared/
│
├── package.json
└── nx.json
Enter fullscreen mode Exit fullscreen mode

Instead of having:

web-repository
admin-repository
api-repository
shared-ui-repository
auth-repository
Enter fullscreen mode Exit fullscreen mode

everything can live inside one repository.

The important idea is:

A monorepo is not simply "one big project."

It is one repository containing multiple independently structured projects.


2. Monorepo vs Polyrepo

There are two common approaches.

Polyrepo

Each application has its own repository.

web/
admin/
api/
mobile/
shared-ui/
Enter fullscreen mode Exit fullscreen mode

Each repository has its own:

  • Git history
  • CI pipeline
  • dependencies
  • configuration
  • releases

This can work well for loosely connected systems.

But sharing code becomes more complicated.

For example:

web ───────> shared-ui repository

admin ─────> shared-ui repository

api ───────> shared-types repository
Enter fullscreen mode Exit fullscreen mode

You may need to publish packages or synchronize versions.


3. The Monorepo Approach

With a monorepo:

platform/
│
├── apps/
│   ├── web
│   ├── admin
│   └── api
│
└── libs/
    ├── ui
    ├── auth
    └── shared-types
Enter fullscreen mode Exit fullscreen mode

Now applications can directly consume internal libraries.

For example:

web
 │
 ├── ui
 ├── auth
 └── api-client
Enter fullscreen mode Exit fullscreen mode

And:

admin
 │
 ├── ui
 ├── auth
 └── api-client
Enter fullscreen mode Exit fullscreen mode

This makes code sharing much easier.


4. What Is Nx?

Nx is a build system and developer platform designed for managing large repositories containing multiple applications and libraries.

Nx provides capabilities such as:

  • Project management
  • Dependency graphs
  • Code generators
  • Task orchestration
  • Computation caching
  • Affected project detection
  • Workspace organization
  • Plugin integrations
  • CI optimization

Nx supports many technologies, including:

  • Angular
  • React
  • Node.js
  • NestJS
  • Next.js
  • Express
  • Vite
  • Jest
  • Cypress
  • Playwright

The important distinction is:

Nx is not a programming framework.

It sits above your applications and helps manage how your projects are organized, built, tested, and deployed.


5. Why Do We Need Nx?

Imagine a repository with:

apps/
├── customer-web
├── admin-web
├── api
├── worker
└── mobile-api

libs/
├── ui
├── auth
├── database
├── shared
├── validation
└── api-client
Enter fullscreen mode Exit fullscreen mode

Suppose you change:

libs/auth
Enter fullscreen mode Exit fullscreen mode

Which applications are affected?

Potentially:

customer-web
admin-web
api
Enter fullscreen mode Exit fullscreen mode

But maybe:

worker
Enter fullscreen mode Exit fullscreen mode

doesn't depend on authentication.

Without dependency awareness, you might run:

npm test
npm build
Enter fullscreen mode Exit fullscreen mode

for everything.

That's inefficient.

Nx understands the relationship between projects.


6. Nx Dependency Graph

One of the most powerful Nx concepts is the project graph.

Imagine:

                shared
               /      \
              /        \
          auth          ui
           |             |
           |             |
          web           admin
Enter fullscreen mode Exit fullscreen mode

Nx knows these relationships.

For example:

web
 ├── auth
 ├── ui
 └── shared
Enter fullscreen mode Exit fullscreen mode

When you change auth, Nx can determine which projects depend on it.

This becomes extremely useful in large repositories.


7. The Nx Workspace

An Nx workspace is the environment where your projects live.

A simplified structure might look like:

my-workspace/
│
├── apps/
├── libs/
├── nx.json
├── package.json
├── tsconfig.base.json
└── node_modules/
Enter fullscreen mode Exit fullscreen mode

The exact structure can vary depending on the Nx version and plugins you're using.

The important part is that Nx knows about the projects inside the workspace.


8. Creating an Nx Workspace

You can create an Nx workspace using:

npx create-nx-workspace@latest
Enter fullscreen mode Exit fullscreen mode

Nx will ask questions about the type of workspace you want.

For example, you may choose:

Angular
React
Node
Integrated Monorepo
Package-based
Enter fullscreen mode Exit fullscreen mode

The choice depends on your architecture.


9. Integrated vs Package-Based Monorepo

This distinction is important.

Integrated Monorepo

Nx manages applications and libraries as projects inside one workspace.

Example:

apps/
libs/
Enter fullscreen mode Exit fullscreen mode

Applications can consume libraries using workspace aliases.

For example:

import { User } from '@company/shared';
Enter fullscreen mode Exit fullscreen mode

This approach is particularly convenient for large application platforms.


Package-Based Monorepo

Another approach is to structure projects more like independently publishable packages.

For example:

packages/
├── ui
├── auth
├── api-client
└── utils
Enter fullscreen mode Exit fullscreen mode

Each package may have its own:

package.json
Enter fullscreen mode Exit fullscreen mode

This can be useful when libraries need stronger package boundaries or may eventually be published externally.


10. Applications vs Libraries

A good Nx architecture separates applications from reusable libraries.

For example:

apps/
├── web
├── admin
└── api

libs/
├── auth
├── ui
├── database
└── shared
Enter fullscreen mode Exit fullscreen mode

Applications represent deployable products.

Libraries represent reusable functionality.


11. What Should Go Inside an App?

An application should contain application-specific behavior.

For example:

apps/web/
├── src/
│   ├── app/
│   ├── main.ts
│   └── styles.css
Enter fullscreen mode Exit fullscreen mode

The application should not become a giant dumping ground for reusable code.

Instead of:

apps/web/src/app/
├── auth/
├── users/
├── products/
├── shared/
├── ui/
└── utils/
Enter fullscreen mode Exit fullscreen mode

you can move reusable functionality into libraries.


12. Libraries

Libraries allow you to isolate functionality.

For example:

libs/
├── auth/
├── users/
├── products/
├── ui/
└── shared/
Enter fullscreen mode Exit fullscreen mode

Now multiple applications can reuse them.

web ───────> auth
           ├── ui
           └── shared

admin ─────> auth
           ├── ui
           └── shared
Enter fullscreen mode Exit fullscreen mode

This reduces duplication.


13. Example: Angular + Node.js Nx Monorepo

A very realistic architecture could look like this:

company-platform/
│
├── apps/
│   ├── web/
│   ├── admin/
│   ├── api/
│   └── worker/
│
├── libs/
│   ├── auth/
│   ├── users/
│   ├── products/
│   ├── database/
│   ├── api-client/
│   ├── ui/
│   └── shared-types/
│
├── nx.json
├── package.json
└── tsconfig.base.json
Enter fullscreen mode Exit fullscreen mode

Here:

web
Enter fullscreen mode Exit fullscreen mode

could be Angular.

admin
Enter fullscreen mode Exit fullscreen mode

could also be Angular.

api
Enter fullscreen mode Exit fullscreen mode

could be NestJS.

worker
Enter fullscreen mode Exit fullscreen mode

could be a Node.js background worker.

And they can share libraries.


14. Shared Types

Suppose the backend returns:

interface User {
  id: string;
  name: string;
  email: string;
}
Enter fullscreen mode Exit fullscreen mode

Instead of defining this independently in:

web
admin
api
Enter fullscreen mode Exit fullscreen mode

we can create:

libs/shared-types
Enter fullscreen mode Exit fullscreen mode

Then:

export interface User {
  id: string;
  name: string;
  email: string;
}
Enter fullscreen mode Exit fullscreen mode

The frontend can consume it:

import { User } from '@company/shared-types';
Enter fullscreen mode Exit fullscreen mode

The backend can consume the same type:

import { User } from '@company/shared-types';
Enter fullscreen mode Exit fullscreen mode

Now both sides use the same contract.


15. Generators

One of Nx's most useful features is its generators.

Instead of manually creating:

library
components
services
tests
configuration
Enter fullscreen mode Exit fullscreen mode

Nx can generate them consistently.

For example:

nx generate library auth
Enter fullscreen mode Exit fullscreen mode

or:

nx g lib auth
Enter fullscreen mode Exit fullscreen mode

Depending on the installed plugins and configuration, Nx will create the appropriate project structure.

Generators help enforce consistency across teams.


16. Why Generators Matter

Imagine ten developers working on a large monorepo.

Without standards, one developer creates:

libs/auth/src/
Enter fullscreen mode Exit fullscreen mode

another creates:

shared/auth/
Enter fullscreen mode Exit fullscreen mode

another creates:

packages/auth/
Enter fullscreen mode Exit fullscreen mode

another uses a different naming convention.

Over time, architecture becomes inconsistent.

Generators help establish a standard.


17. Nx Tasks

Nx manages tasks such as:

build
test
lint
serve
e2e
Enter fullscreen mode Exit fullscreen mode

For example:

nx build web
Enter fullscreen mode Exit fullscreen mode

or:

nx test auth
Enter fullscreen mode Exit fullscreen mode

or:

nx lint api
Enter fullscreen mode Exit fullscreen mode

You can also run tasks across multiple projects.

nx run-many -t build
Enter fullscreen mode Exit fullscreen mode

This tells Nx to execute the build target across projects.


18. The affected Concept

This is one of the most important concepts in Nx.

Suppose you modify:

libs/auth
Enter fullscreen mode Exit fullscreen mode

and these applications depend on it:

web
admin
Enter fullscreen mode Exit fullscreen mode

But:

worker
Enter fullscreen mode Exit fullscreen mode

doesn't.

Instead of rebuilding everything, Nx can determine the affected projects.

For example:

nx affected -t build
Enter fullscreen mode Exit fullscreen mode

This effectively means:

Build the projects affected by the changes.

Similarly:

nx affected -t test
Enter fullscreen mode Exit fullscreen mode

can run tests only where they are needed.


19. Why affected Is Important in CI

Imagine a monorepo with:

20 applications
100 libraries
Enter fullscreen mode Exit fullscreen mode

A developer changes:

libs/button
Enter fullscreen mode Exit fullscreen mode

Running every test could be expensive.

Nx can analyze the dependency graph and focus on affected projects.

Conceptually:

Git changes
     ↓
Nx project graph
     ↓
Affected projects
     ↓
Build / Test / Lint
Enter fullscreen mode Exit fullscreen mode

This can dramatically improve CI efficiency.


20. Nx Caching

Another powerful feature is task caching.

Imagine you run:

nx test auth
Enter fullscreen mode Exit fullscreen mode

The test takes:

30 seconds
Enter fullscreen mode Exit fullscreen mode

Later you run the exact same task without changing its inputs.

Nx can reuse the previous result.

Conceptually:

Task
 ↓
Calculate inputs
 ↓
Check cache
 ↓
Cache hit?
 ├── Yes → Restore result
 └── No  → Execute task
Enter fullscreen mode Exit fullscreen mode

Instead of:

30 seconds
Enter fullscreen mode Exit fullscreen mode

you may get the result almost immediately.


21. Local Cache

Nx can cache results locally.

For example:

.nx/
Enter fullscreen mode Exit fullscreen mode

contains Nx-related workspace data and cache information.

The exact cache structure depends on the Nx version and configuration.

The important idea is:

If Nx knows the task inputs haven't changed, it can reuse the previous result.


22. Distributed / Remote Caching

For teams, local caching isn't always enough.

Imagine:

Developer A
    ↓
build
    ↓
cache
Enter fullscreen mode Exit fullscreen mode

Developer B pulls the same code.

Without shared caching:

Developer B
    ↓
build again
Enter fullscreen mode Exit fullscreen mode

With remote caching:

Developer A
      ↓
 Remote Cache
      ↑
Developer B
Enter fullscreen mode Exit fullscreen mode

Developer B can reuse results generated by Developer A.

This can significantly reduce CI and developer build times.


23. Project Boundaries

Large monorepos have another problem:

What prevents developers from importing everything everywhere?

For example:

admin
  ↓
database
Enter fullscreen mode Exit fullscreen mode

Maybe the admin frontend should never directly access database code.

You want architectural rules such as:

UI
 ↓
Feature
 ↓
Data Access
 ↓
Backend
Enter fullscreen mode Exit fullscreen mode

Nx supports mechanisms such as tags and module boundary enforcement to help control these relationships.


24. Example of Architectural Boundaries

Imagine these tags:

type:app
type:feature
type:data-access
type:ui
type:util
Enter fullscreen mode Exit fullscreen mode

And domains:

scope:users
scope:products
scope:shared
Enter fullscreen mode Exit fullscreen mode

Now you can define rules such as:

feature → ui
feature → data-access
data-access → util
app → feature
Enter fullscreen mode Exit fullscreen mode

But prevent:

ui → feature
Enter fullscreen mode Exit fullscreen mode

or:

frontend → database
Enter fullscreen mode Exit fullscreen mode

This transforms the monorepo from simply being a shared folder into an architecture with enforceable rules.


25. Domain-Driven Organization

A large Nx workspace can benefit from domain-based organization.

Instead of:

libs/
├── components
├── services
├── models
├── utils
Enter fullscreen mode Exit fullscreen mode

you could have:

libs/
├── users/
│   ├── feature/
│   ├── data-access/
│   ├── ui/
│   └── util/
│
├── products/
│   ├── feature/
│   ├── data-access/
│   └── ui/
│
└── shared/
    ├── ui/
    └── util/
Enter fullscreen mode Exit fullscreen mode

This keeps related functionality together.


26. Nx and Angular

Nx works particularly well with Angular.

For example:

apps/
├── customer
└── admin

libs/
├── users
├── products
├── auth
└── ui
Enter fullscreen mode Exit fullscreen mode

Angular applications can consume shared Angular libraries.

For example:

import { UserCardComponent } from '@company/ui';
Enter fullscreen mode Exit fullscreen mode

And:

import { AuthService } from '@company/auth';
Enter fullscreen mode Exit fullscreen mode

This becomes very useful when multiple Angular applications share the same design system and business logic.


27. Nx and React

The same architectural principles apply to React.

For example:

apps/
├── storefront
└── dashboard

libs/
├── ui
├── auth
├── api-client
└── shared
Enter fullscreen mode Exit fullscreen mode

A shared React component:

export function Button() {
  return <button>Click me</button>;
}
Enter fullscreen mode Exit fullscreen mode

can be consumed by multiple applications.


28. Nx and Node.js

Nx isn't only for frontend applications.

You can have:

apps/
├── api
├── worker
├── notifications
└── cron
Enter fullscreen mode Exit fullscreen mode

with shared libraries:

libs/
├── database
├── auth
├── config
├── logging
└── shared-types
Enter fullscreen mode Exit fullscreen mode

For example:

api
 ↓
database
 ↓
shared
Enter fullscreen mode Exit fullscreen mode

and:

worker
 ↓
database
 ↓
shared
Enter fullscreen mode Exit fullscreen mode

This is particularly useful for backend platforms containing multiple services.


29. A Production-Style Architecture

Consider an e-commerce platform.

We might have:

apps/
├── storefront
├── admin
├── api
├── worker
└── notification-service

libs/
├── auth/
├── users/
├── products/
├── orders/
├── payments/
├── database/
├── messaging/
├── api-client/
├── shared-types/
└── ui/
Enter fullscreen mode Exit fullscreen mode

Dependency relationships could look like:

                    shared-types
                   /     |      \
                  /      |       \
             storefront admin    api
                 |        |       |
                ui       auth    database
                 \        |       /
                  \       |      /
                    shared libs
Enter fullscreen mode Exit fullscreen mode

This creates a structured development platform.


30. Monorepo Does Not Mean Microservices

This is an important distinction.

You can have:

Monorepo + Monolith
Enter fullscreen mode Exit fullscreen mode

or:

Monorepo + Microservices
Enter fullscreen mode Exit fullscreen mode

For example:

apps/
├── api
└── web
Enter fullscreen mode Exit fullscreen mode

could be a simple full-stack monorepo.

But:

apps/
├── users-service
├── orders-service
├── payments-service
├── notification-service
└── web
Enter fullscreen mode Exit fullscreen mode

could represent microservices in a monorepo.

The repository strategy and deployment architecture are separate concepts.


31. Monorepo + Microservices

One of the most interesting architectures is:

                 Nx Monorepo
                     |
        ┌────────────┼────────────┐
        ↓            ↓            ↓
   Users Service Orders Service Payments Service
        |            |            |
        └────────────┼────────────┘
                     ↓
                Shared Libraries
Enter fullscreen mode Exit fullscreen mode

Each service can be deployed independently.

For example:

users-service → Docker container
orders-service → Docker container
payments-service → Docker container
Enter fullscreen mode Exit fullscreen mode

while all source code remains in one repository.


32. What Should You NOT Share?

A common monorepo mistake is sharing too much.

For example:

libs/shared
Enter fullscreen mode Exit fullscreen mode

can become a giant dumping ground:

shared/
├── constants
├── models
├── services
├── database
├── auth
├── ui
├── random-utils
└── everything-else
Enter fullscreen mode Exit fullscreen mode

This creates hidden coupling.

A better approach is to create focused libraries:

libs/
├── auth
├── database
├── ui
├── users
├── orders
└── shared-types
Enter fullscreen mode Exit fullscreen mode

Each library should have a clear responsibility.


33. The Danger of Circular Dependencies

Consider:

auth → users
users → auth
Enter fullscreen mode Exit fullscreen mode

Now we have:

auth
 ↑  ↓
users
Enter fullscreen mode Exit fullscreen mode

This creates a circular dependency.

Circular dependencies make systems harder to understand, test, and evolve.

Nx's project graph can help identify these relationships.

A good architecture generally aims for directional dependencies.

For example:

UI
 ↓
Feature
 ↓
Data Access
 ↓
Infrastructure
Enter fullscreen mode Exit fullscreen mode

rather than cycles.


34. Nx in CI/CD

A strong CI pipeline might look like:

Pull Request
      ↓
Detect changed files
      ↓
Nx affected
      ↓
Lint
      ↓
Test
      ↓
Build
      ↓
Deploy affected applications
Enter fullscreen mode Exit fullscreen mode

Instead of rebuilding the entire monorepo for every pull request.

For example:

nx affected -t lint
nx affected -t test
nx affected -t build
Enter fullscreen mode Exit fullscreen mode

This is one of the biggest advantages of Nx for large teams.


35. Nx and Docker

Nx can also work well with Docker-based deployments.

Suppose:

apps/
├── web
├── api
└── worker
Enter fullscreen mode Exit fullscreen mode

You may create separate Docker images:

web → web image
api → api image
worker → worker image
Enter fullscreen mode Exit fullscreen mode

The repository remains unified while deployment remains independent.

This gives you:

Single source repository
+
Independent deployment
Enter fullscreen mode Exit fullscreen mode

36. When Should You Use Nx?

Nx is particularly valuable when:

  • You have multiple applications
  • You share libraries between applications
  • You have Angular/React/Node projects together
  • Your repository is becoming large
  • CI builds are becoming slow
  • You need architectural boundaries
  • Multiple teams work on the same codebase
  • You want standardized project generation
  • You want dependency-aware builds

37. When Nx Might Be Overkill

If you have:

one small React application
Enter fullscreen mode Exit fullscreen mode

with:

10 components
Enter fullscreen mode Exit fullscreen mode

and no shared libraries, Nx may provide more infrastructure than you actually need.

A simple Vite/Angular/Next.js project might be enough.

The goal isn't:

"Use Nx because large companies use it."

The goal is:

"Use Nx when repository complexity justifies centralized project orchestration."


38. A Useful Mental Model

Think about Nx as a layer above your applications.

Without Nx:

Git
 |
Applications
 |
Libraries
Enter fullscreen mode Exit fullscreen mode

With Nx:

                 Nx
                  |
      ┌───────────┼───────────┐
      ↓           ↓           ↓
   Projects     Tasks       Graph
      ↓           ↓           ↓
 Applications  Build/Test  Dependencies
      |
   Libraries
Enter fullscreen mode Exit fullscreen mode

Nx doesn't replace Angular, React, Node.js, TypeScript, or your backend framework.

It coordinates them.


39. The Real Value of Nx

The biggest benefit of Nx isn't the CLI.

It's the combination of:

Project Graph
+
Task Orchestration
+
Caching
+
Affected Detection
+
Generators
+
Architectural Boundaries
Enter fullscreen mode Exit fullscreen mode

Together, these features allow a monorepo to scale without becoming completely chaotic.


40. Final Architecture

A mature Nx monorepo might eventually look like:

platform/
│
├── apps/
│   ├── storefront/
│   ├── admin/
│   ├── api/
│   ├── worker/
│   └── notification-service/
│
├── libs/
│   ├── users/
│   │   ├── feature/
│   │   ├── data-access/
│   │   └── ui/
│   │
│   ├── orders/
│   │   ├── feature/
│   │   ├── data-access/
│   │   └── ui/
│   │
│   ├── auth/
│   ├── payments/
│   ├── database/
│   ├── messaging/
│   ├── api-client/
│   ├── shared-types/
│   └── ui/
│
├── nx.json
├── package.json
└── tsconfig.base.json
Enter fullscreen mode Exit fullscreen mode

And the development workflow becomes:

Developer changes code
        ↓
Nx analyzes dependencies
        ↓
Affected projects identified
        ↓
Only necessary tasks execute
        ↓
Cache reused where possible
        ↓
CI becomes faster
        ↓
Applications deploy independently
Enter fullscreen mode Exit fullscreen mode

Conclusion

Nx is much more than a tool for putting multiple projects inside one Git repository.

It provides an architectural and build-system layer that helps teams manage complexity as their codebase grows.

The most important concepts to understand are:

  1. Monorepo — multiple applications and libraries in one repository.
  2. Project Graph — understanding relationships between projects.
  3. Libraries — reusable and isolated functionality.
  4. Generators — consistent project creation.
  5. Affected Commands — execute work only where changes matter.
  6. Caching — avoid repeating expensive work.
  7. Boundaries — enforce architectural rules.
  8. CI Optimization — reduce unnecessary builds and tests.
  9. Independent Deployment — keep one repository without forcing one deployment unit.
  10. Scalable Architecture — organize applications around domains and responsibilities.

The real power of Nx appears when a codebase becomes large enough that dependency management, CI performance, code sharing, and architectural consistency become difficult to handle manually.

That's where Nx turns a growing monorepo from a collection of projects into a managed development platform.

A monorepo gives you one repository. Nx gives you the tools to make that repository scalable.


What's Next?

If you're learning Nx seriously, the next step is to build a real project rather than only learning the commands.

A great practical project would be:

Angular Storefront
        +
Angular Admin Dashboard
        +
NestJS API
        +
Node.js Worker
        +
Shared Authentication
        +
Shared UI Library
        +
Shared TypeScript Contracts
        +
Docker
        +
CI/CD
Enter fullscreen mode Exit fullscreen mode

That project will demonstrate why Nx becomes valuable when the architecture starts getting large.

Top comments (0)