Blog
Engineering
6
minutes

Nx Architecture Part-1: Organizing and Structuring a React Project with Nx

We have previously published an article discussing the benefits of using Nx. You can find it here. This article is intended for individuals with experience using Nx. However, if you want to learn how to structure a mid-size or large React application using Nx, this article is for you. The article will be divided into two parts: one will provide an overview of Nx concepts and recommendations based on the official documentation, and the other will present a practical example of how to structure an Nx project with React, based on my experience with multiple Nx + React projects.
Camille Tjhoa
Senior Software Engineer
Summary
Twitter icon
linkedin icon

Key concepts

Before diving deep into example, we should clarify some concepts

Applications and Libraries

In Nx, applications and libraries are two key concepts. Applications (living under /apps directory) are responsible for routing, configuring dependency injection and connecting libraries. They should not include any components, services, or business logic. Instead, their role is to orchestrate the libraries to build the product you want.

On the other hand, libraries (living under /libs directory) contain the reusable components, services, and business logic that can be shared across multiple applications. They can be organized based on functionality or domain, allowing for better organization, reusability, and code maintainability.

Note that you can now customize those folders https://nx.dev/reference/nx-json#workspace-layout. By separating the concerns of applications and libraries, Nx allows for better modularity and scalability of projects. It enables developers to build applications by composing different libraries and enforce project boundaries to ensure clean separation and control over dependencies.

The 80/20 rule: Note that a typical Nx workspace has many more libs than apps, following an 80/20 approach:

  • About 80% of the logic is placed in the /libs folder.
  • About 20% is placed in the /apps folder.

A typical way to conceptualize the application is as "containers" that connect, package, and build functionality implemented in libraries. So pay attention to the organization of the libs directory is really important because it roughly represents 80% of your codebase. The official documentation [1] provides more information on applications and libraries in Nx.

Boundaries and Tags dimensions

In large workspace, it is common to have multiple teams working on different parts of the solution. These projects are often divided into logical domains, with each team focusing on a specific domain. Within each domain, there is usually a clear public API that allows other domains to consume the information.

Defining and enforcing project boundaries are key elements in an Nx workspace. They help organize and maintain code and provide control over dependencies.

Nx allows you to enforce module boundaries using ESlint rules based on tags. By doing so, developers can ensure clean separation between different application parts and prevent unintended dependencies[2].

Tags are basically labels in project.json or package.json files and you can come up with whatever you want but it has to be static string. Beware, tags aren’t folders! You can create tag dimension using semi-colon. Eg. foo:bar foo:baz qux:quux qux:corge ****will give you a foo dimension and a qux dimension (both with multiple values).

It’s important to define tags dimensions to have flexibility and control over communication between projects inside your workspace[3].

Note that you have several formats to fine-tune your ESlint rules: https://nx.dev/core-features/enforce-module-boundaries#tag-formats. Remember that the more tags you introduce, the more difficult it becomes to reason about your communication rules.

Code Organization & Naming Conventions

Now that we have clarified key concepts, we can dig into the next level and see some implementations.

Tags: Scope vs Types

Based on the documentation[4], the Nx team recommends 2 tag dimensions that should not be confused.

  • Type which defines "What is in this library”. eg. type:foo
  • Scope which defines "Where a library lives", "What visibility a library has", “who owns it”. eg. scope:foo

The combination of both allows us to enforce boundaries between projects.

Workspace structure

In an NX workspace, projects (apps and libs) are organized around folders and NX tags.

A general naming convention is to use naming prefixes for library types and folders for library scopes.

eg.

libs/
└── shared/
└── util-dates/
└── src/
├── index.ts
└── lib/

util-dates is a utility-type library with a shared visibility scope.

Library types

Within an Nx workspace, you'll encounter various types of libraries. To ensure order and maintainability, Nx suggests limiting the number of library types to a smaller set. Remember that you must document what each type of library means.

For my part, I like to stick with the types given in the documentation[3][5], with some subtleties on the data-access libraries we will see later.

null

*Note: our definition differs slightly from the Nx documentation because it partially excludes the state management from this type. This subject will be addressed in part 2.

Nx also shows some examples where Applications can have a scope:app tag to enforce boundaries on Applications, too.

Library scopes

In the context of Nx, libraries can be scoped based on functionality or domain. This means that libraries can be grouped together based on their purpose or the specific aspect of the application they are responsible for. Structuring your application into folders that represent scope will help you define code ownership and boundaries. [4]

null

Here is an example of an Nx graph using those concepts:

null

As you can see, domains are split and independent of one another. You can also have shared projects for standard components or utils.

Here is the corresponding folder structure

libs/
├── shared/
│ ├── ui/
│ │ └── src/
│ │ ├── index.ts
│ │ └── lib/
│ └── util-js/
│ └── src/
│ ├── index.ts
│ └── lib/
└── domains/
├── clusters
│ ├── data-access
│ │ └── src
│ │ ├── index.ts
│ │ └── lib
│ └── feature
│ └── src
│ ├── index.ts
│ └── lib
└── cloud-providers
├── data-access
│ └── src
│ ├── index.ts
│ └── lib
└── feature
└── src
├── index.ts
└── lib

Conclusion

We’ve seen some of advanced architecture concepts in Nx, specifically focusing on structuring and organizing a mid-size/large React application. You can already use to drive your teams workflow and establish boundaries between them.

You will benefit from a better separation of concern and an overview of your workspace's internal dependencies.

Nx provides powerful tools and concepts such as tags dimensions and library types to build a controlled system that works for your case

The second part of the article will provide a practical example of structuring an Nx project with React using tanstack-query and showing boundaries enforcement.

Side note: Opinionated guide

I do want to highlight that the Nx documentation doesn’t give you really strict guidance on how to structure your project, especially on React. There are multiple reasons for that.

First, Nx is not framework or library-oriented, so it tends to be as generic as possible. Nx is not even JS or language-oriented. It is a tool that is completely agnostic and focuses on dependency management. Unfortunately, this also means that documentation rarely goes into detail for complex use cases, and you often feel short on how to structure your app. However, the Nx team is making reasonable efforts with videos and interviews on their YouTube channel and adding AI chat to fill that gap.

In addition, Nx's main target in the JS/TS world used to be Angular. Due to historical reasons and/or community alignment, React is second regarding documentation/blog posts/examples you can glance at on the web. Even so, it's worth noting that the situation has improved.

Remember that no one-size-fits-all solution exists, and your project structure should be tailored to your specific needs and workflows.

References

  1. https://nx.dev/concepts/more-concepts/applications-and-libraries
  2. https://blog.nrwl.io/mastering-the-project-boundaries-in-nx-f095852f5bf4
  3. https://nx.dev/recipes/other/tag-multiple-dimensions
  4. https://nx.dev/more-concepts/monorepo-nx-enterprise
  5. https://nx.dev/more-concepts/library-types
  6. https://nx.dev/core-features/enforce-project-boundaries
  7. https://nx.dev/more-concepts/grouping-libraries
  8. https://www.jimmybogard.com/vertical-slice-architecture/
  9. https://github.com/juanm4/hexagonal-architecture-frontend
Share on :
Twitter icon
linkedin icon
Ready to rethink the way you do DevOps?
Qovery is a DevOps automation platform that enables organizations to deliver faster and focus on creating great products.
Book a demo

Suggested articles

DevOps
 minutes
Best 10 VMware alternatives: the DevOps guide to escaping the "Broadcom Tax"

Facing VMware price hikes after the Broadcom acquisition? Explore the top 10 alternatives - from Proxmox to Qovery, and discover why leading teams are switching from legacy VMs to modern DevOps automation.

Mélanie Dallé
Senior Marketing Manager
DevOps
DevSecOps
 minutes
Zero-friction DevSecOps: get instant compliance and security in your PaaS pipeline

Shifting security left shouldn't slow you down. Discover how to achieve "Zero-Friction DevSecOps" by automating secrets, compliance, and governance directly within your PaaS pipeline.

Mélanie Dallé
Senior Marketing Manager
DevOps
Observability
Heroku
 minutes
Deploy to EKS, AKS, or GKE without writing a single line of YAML

Stop choosing between Heroku's simplicity and Kubernetes' power. Learn how to deploy to EKS, GKE, or AKS with a PaaS-like experience - zero YAML required, full control retained.

Mélanie Dallé
Senior Marketing Manager
DevOps
Platform Engineering
 minutes
GitOps vs. DevOps: how can they work together?

Is it GitOps or DevOps? Stop choosing between them. Learn how DevOps culture and GitOps workflows work together to automate Kubernetes, eliminate drift, and accelerate software delivery.

Mélanie Dallé
Senior Marketing Manager
DevSecOps
Platform Engineering
Internal Developer Platform
 minutes
Cut tool sprawl: automate your tech stack with a unified platform

Stop letting tool sprawl drain your engineering resources. Discover how unified automation platforms eliminate configuration drift, close security gaps, and accelerate delivery by consolidating your fragmented DevOps stack.

Mélanie Dallé
Senior Marketing Manager
DevOps
Developer Experience
 minutes
Top 10 GitHub actions alternatives: stop optimizing for "price per minute"

GitHub’s new self-hosted fees are a wake-up call. But moving to the "cheapest" runner provider is a strategic error. Discover the top alternatives that optimize for Total Cost of Ownership, not just compute costs.

Mélanie Dallé
Senior Marketing Manager
Product
Observability
5
 minutes
Alerting with guided troubleshooting in Qovery Observe

Get alerted and fix issues with full context. Qovery Observe notifies you when something goes wrong and guides you straight to the metrics and signals that explain why, all in one place.

Alessandro Carrano
Head of Product
DevOps
Developer Experience
 minutes
Top 10 Terraform cloud alternatives: the DevOps guide to escaping "State File Nightmares"

Tired of rising licensing costs and complex state management? Discover the top 10 alternatives to modernize your Infrastructure as Code, empower developers, and regain control of your cloud spend.

Mélanie Dallé
Senior Marketing Manager

It’s time to rethink
the way you do DevOps

Turn DevOps into your strategic advantage with Qovery, automating the heavy lifting while you stay in control.