Node Monorepos with Nx

Rate this content
Bookmark
Github
The workshop video on Node Monorepos with Nx covers various aspects of managing monorepos efficiently. It starts by explaining the integrated model repo that allows multiple applications in the same repository. The video highlights the use of plugins and code generators in Nx, and how to manage them using commands like 'nx list' and 'nx generate'. It dives into the benefits of using monorepos, such as atomic changes, easy code sharing, and simplified dependency management. The video also discusses the creation of a Node API app, running executors, and setting up CI workflows. Nx Cloud's distributed caching and task execution are also covered, showcasing how it optimizes build and test processes. The video emphasizes that Nx is framework and language agnostic, supporting various languages and frameworks. Additionally, it touches upon the importance of dependency graphs and module boundaries for efficient code management.

From Author:

Multiple apis and multiple teams all in the same repository can cause a lot of headaches, but Nx has you covered. Learn to share code, maintain configuration files and coordinate changes in a monorepo that can scale as large as your organisation does. Nx allows you to bring structure to a repository with hundreds of contributors and eliminates the CI slowdowns that typically occur as the codebase grows.


Table of contents:

- Lab 1 - Generate an empty workspace

- Lab 2 - Generate a node api

- Lab 3 - Executors

- Lab 4 - Migrations

- Lab 5 - Generate an auth library

- Lab 6 - Generate a database library

- Lab 7 - Add a node cli

- Lab 8 - Module boundaries

- Lab 9 - Plugins and Generators - Intro

- Lab 10 - Plugins and Generators - Modifying files

- Lab 11 - Setting up CI

- Lab 12 - Distributed caching

This workshop has been presented at Node Congress 2023, check out the latest edition of this Tech Conference.

FAQ

Node Monorepos with NX is a workshop tailored towards using Node.js as the framework within NX, which is framework and language agnostic. It focuses on managing codebases in a monorepo setup, where multiple applications can coexist in a single repository, improving code sharing and dependency management.

The benefits of using a monorepo include atomic changes across multiple apps, easier code sharing, and a single set of dependencies which simplifies management and reduces conflicts between different versions of the same dependency.

Yes, NX is framework and language agnostic. It can be used with front-end frameworks like React or Angular, and it supports various programming languages including Java, Python, and .NET.

A monorepo involves managing multiple projects within a single repository with tooling support to handle complexities like dependency management and testing scopes. Code colocation, on the other hand, is simply placing multiple projects in one repository without any specialized tooling, which can lead to maintenance and scalability issues.

NX improves efficiency by using techniques like affected command detection to run tests and builds only on affected projects, caching results for faster subsequent builds, and providing tools for controlled code sharing and consistent coding practices across teams.

In a monorepo setup, NX can detect changes in a shared library and automatically run tests for dependent applications. This immediate feedback loop helps developers fix issues quicker, ensuring that changes in one part do not adversely affect other parts of the application.

NX supports distributed caching through NX Cloud, which allows build artifacts to be shared across a team or organization. This means that if one developer builds a project, the results can be reused by others, significantly reducing the build time across the team.

Isaac Mann
Isaac Mann
160 min
06 Apr, 2023

Comments

Sign in or register to post your comment.

Video Transcription

Available in Español: Monorepos de Node con Nx

1. Introduction to Node Monorepos with NX

Short description:

Welcome to Node Monorepos with NX. This workshop will be tailored towards Node as the framework used with NX. NX is framework and language agnostic, allowing you to use front-end applications like React or Angular, as well as other languages like Java and Python. Monorepos offer benefits such as atomic changes, easy code sharing, and simplified dependency management.

All right, okay, so welcome to Node Monorepos with NX. This is great. So I am, my name is Isaac Mann. I'm an architect at NX. We've been, I've been working with NX for four years. I've been an employee of NX for four years. I used it for two years before that. And yeah, this is exciting.

So this workshop will be tailored towards Node as just the framework that we'll be using with NX. But NX is, you know, is pretty much framework and language agnostic. But we'll get into that more. So it's, you know, you can use front-end applications like React or Angular or you know, whatever other front-end technology you're using. You can also use other languages like obviously people use Java and Python and.NET with NX. So NX is all about, you know, managing your code base and whatever code you're using is this is node monorepos with NX.

And first, we're going to start with a little discussion of, why you would want a monorepo. So a monorepo is any repository that has more than one application living in that, in that codebase. So what's the benefit of a monorepo? So three kind of big categories of benefits for monorepos. Monorepo give you atomic changes, allow you to easily share your code and give you a single set of dependencies, make it easier to manage those dependencies. So we're going to dig into each of these three things and explain what they are and why they're beneficial.

2. Benefits of Monorepo and NX

Short description:

A monorepo offers benefits such as atomic changes, easy code sharing, and simplified dependency management. It allows for faster command execution, targeted testing for affected projects, and local and distributed caching. NX provides tooling to help achieve the benefits of a monorepo without the drawbacks of code colocation. It enables controlled code sharing and efficient management of dependencies.

So if you had an application and a kind of a library that is, that is used by that application. And if you have them managed in two separate repos, let's say you make a change to the UI library that's in repo two at the bottom here. If you make a change to it, and for some reason it breaks the homepage application, you, the, the lifecycle of that change would be so somebody makes a change to the common UI library. They push up the commit, they incommit the version at some point later somebody tries to use that new version of the common UI library in the homepage app. They realize there's a, there's a breaking change there and it broke their application. So they file a bug with the common UI library, and they say, Hey, you gotta fix this. And then later once the UI library developer sees that she sees that problem they, you know, they make the change, they push a new fix up new version, and then the homepage app, people later have to you know bump the version to the fixed fixed version, and then see if it see if it fix their application. So that that whole cycle, typically, I mean the best case scenario few days, it can take a week or two if if people are not you know working on these things full time.

So, one of the issues here is, if you were to put both those library, the library and the application together in the same repo, then that that cycle would be, it would be just the UI library creator, making a change, running the tests and seeing Oh, it broke the tests in the homepage app, I've got to fix that. And that so that that cycle took will take like a half hour an hour. Like, even before you've pushed up a PR, you'd notice that you broke the application, you have to make, you know, modify your code. So, that's so you have in one PR, you know, whether you know, all the changes that you need to make to the application or changes that you know, you need to make the UI library to adapt to the application itself. That's that's one benefit of a mono repo. Second benefit is shared code. Let's say you have some logic about you know, what is a valid username? And you know, this function, it's obviously, the different scenarios, you'd have a, you know, more complex function than this, you're handling whether username was valid. But let's say you want to share this across your application across multiple application, multiple libraries. If this ever were to change, you'd have to update that in every single repo that's using it. If you're if you're copying the code from repo to repo, whereas if you're in a monorepo setting, you can just use this function. And whatever that function changes, then your, your behavior changes across the whole, the whole repository, wherever it's being used. So easily sharing code. Third thing is having a single set of dependencies. So if you have a framework like Node, or in this case, and the picture is react, if you have different versions of that framework being used in different applications, you can run into very strange, hard to debug runtime errors. And, and having your code in the same repository will basically forces you to have, well it can you can set rules in place to force yourself to be on the same version for everything that's in that repo. It is possible to have multiple versions of the same mono repo, but it's, it's better to have a single version and make things easier in the long run. So basically in in a large when you have multiple applications, typically you have one application that you're working on all the time, that it's on the latest version of whatever dependencies you you have, but if you, you're going to have, you know, two or three other applications that they get worked on maybe once every couple of months and those inevitably fall behind and you, you have to remember, okay, how did I upgrade that framework? Um, you know, this, the six months ago upgrade, and you have to remember and go through all that same work again. Um, whereas if it was, if you were upgrading all your applications, all three applications at the same time, it's a lot easier than upgrading three different applications, different points over the course of a year and a half. Um, so you just do it all at once and it's a lot easier than, than doing it spread out over a year or whatever, whenever you think of updating those applications.

So, one way of, you know, having a, um, a mono repo, um, so the difference between a mono repo and code colocation and code colocation is when you just dump all the applications together in the same repo without having any tooling to manage that. And that, that can be a nightmare. If you just dump it all together, um, you can end up with a scenario where you're running unnecessary tests. We have no code boundaries and all the code just gets mixed up together and it's hard to maintain. Um, and you can have inconsistent tooling where you have conflicts between how those tools interact with each other like your testing tools or your, uh, whatever other tools you have.

So, NX is a tooling to help you, uh, get all the benefits of a monorepo without the negatives of code colocation. All right. So NX gives you a faster command execution. So, um, executors are basically, um, like, uh, NPM scripts, um, that can help you, um, you run your, um, your build and test and lent and all the actions that you take on your code. Uh, NX affected is the tool that, that lets you run the tests for only the projects that are affected by a, uh, by a code change and not the tests that are unrelated. Um, and then we have local and distributed caching. So anytime you run a command, um, and, uh, if you run that same command again without changing the code, it just uses the, instead of actually running the build again, it just uses the, the cast version of that, um, of that output and, um, without, you know, actually, um, running the build again. So if, if your bill takes, you know, a minute and a half, um, if you run that build again, instead of taking a minute, a half, it'll take a second or two just to pool that, that data from the cache and to replay the terminal output for you. Um, so that's just locally. That's, that's completely free. That's in the open source NX. Um, then with a NX cloud, you can use the distributed caching. So that, that same caching behavior that you get, um, on your own machine, you can share that cache across your whole organization. So if anybody anywhere has run this command with this same code, you can use their cache and replay it yourself. So this, you know, particularly for PRS, you, you run your code, you push it up. Um, the runs in CI and then somebody else checks that branch out on their machine and they run the same thing. They can, um, just use your cache version instead of running that same, um, you know, computation on their machine again. Um, so basically you, you only run, um, the actions for a particular set of code one time across your whole organization, which is, um, and that's also, it's not just for, um, uh, that that's broken up by projects. So if you have, um, one application that depends on five different, um, libraries, um, that's the, the library build step is cached as well as the application build steps. So say you change just the application, those five libraries, their build output is already cached for you. And then, so you can just use that cache and then only do the last application built step, which can be game changing for large code bases. Um, and X also gives you controlled code sharing. So you, you set a, an index.ts file at the root of your, um, each project.

Watch more workshops on topic

React at Scale with Nx
React Summit 2023React Summit 2023
145 min
React at Scale with Nx
Top Content
Featured WorkshopFree
Isaac Mann
Isaac Mann
We're going to be using Nx and some its plugins to accelerate the development of this app.
Some of the things you'll learn:- Generating a pristine Nx workspace- Generating frontend React apps and backend APIs inside your workspace, with pre-configured proxies- Creating shared libs for re-using code- Generating new routed components with all the routes pre-configured by Nx and ready to go- How to organize code in a monorepo- Easily move libs around your folder structure- Creating Storybook stories and e2e Cypress tests for your components
Table of contents: - Lab 1 - Generate an empty workspace- Lab 2 - Generate a React app- Lab 3 - Executors- Lab 3.1 - Migrations- Lab 4 - Generate a component lib- Lab 5 - Generate a utility lib- Lab 6 - Generate a route lib- Lab 7 - Add an Express API- Lab 8 - Displaying a full game in the routed game-detail component- Lab 9 - Generate a type lib that the API and frontend can share- Lab 10 - Generate Storybook stories for the shared ui component- Lab 11 - E2E test the shared component

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Levelling up Monorepos with npm Workspaces
DevOps.js Conf 2022DevOps.js Conf 2022
33 min
Levelling up Monorepos with npm Workspaces
Top Content
NPM workspaces help manage multiple nested packages within a single top-level package, improving since the release of NPM CLI 7.0. You can easily add dependencies to workspaces and handle duplications. Running scripts and orchestration in a monorepo is made easier with NPM workspaces. The npm pkg command is useful for setting and retrieving keys and values from package.json files. NPM workspaces offer benefits compared to Lerna and future plans include better workspace linking and adding missing features.
End the Pain: Rethinking CI for Large Monorepos
DevOps.js Conf 2024DevOps.js Conf 2024
25 min
End the Pain: Rethinking CI for Large Monorepos
Today's Talk discusses rethinking CI in monorepos, with a focus on leveraging the implicit graph of project dependencies to optimize build times and manage complexity. The use of NX Replay and NX Agents is highlighted as a way to enhance CI efficiency by caching previous computations and distributing tasks across multiple machines. Fine-grained distribution and flakiness detection are discussed as methods to improve distribution efficiency and ensure a clean setup. Enabling distribution with NX Agents simplifies the setup process, and NX Cloud offers dynamic scaling and cost reduction. Overall, the Talk explores strategies to improve the scalability and efficiency of CI pipelines in monorepos.
Federated Microfrontends at Scale
React Summit 2023React Summit 2023
31 min
Federated Microfrontends at Scale
Top Content
Watch video: Federated Microfrontends at Scale
This Talk discusses the transition from a PHP monolith to a federated micro-frontend setup at Personio. They implemented orchestration and federation using Next.js as a module host and router. The use of federated modules and the integration library allowed for a single runtime while building and deploying independently. The Talk also highlights the importance of early adopters and the challenges of building an internal open source system.
Scale Your React App without Micro-frontends
React Summit 2022React Summit 2022
21 min
Scale Your React App without Micro-frontends
This Talk discusses scaling a React app without micro-frontend and the challenges of a growing codebase. Annex is introduced as a tool for smart rebuilds and computation caching. The importance of libraries in organizing code and promoting clean architecture is emphasized. The use of caching, NxCloud, and incremental build for optimization is explored. Updating dependencies and utilizing profiling tools are suggested for further performance improvements. Splitting the app into libraries and the benefits of a build system like NX are highlighted.
The Age of Monorepos
JSNation 2022JSNation 2022
25 min
The Age of Monorepos
Today's Talk is about the world of monorepos, their history, benefits, and features. Monorepos address challenges in web development, such as slow build processes and unstable connections on mobile devices. Collocation in monorepos enables easy sharing of functions and components among projects. Speed and efficiency in monorepos are achieved through collocation, dependency graphs, and task orchestration. Monorepo tools like Learnr offer features such as caching and distributed task execution. Monorepos provide code sharing, consistent tooling, and automated migration, resulting in a 10x developer experience.
Remixing Your Stack in a Monorepo Workspace
Remix Conf Europe 2022Remix Conf Europe 2022
22 min
Remixing Your Stack in a Monorepo Workspace
Let's talk about remixing our stack in a Monorepo workspace, which allows for incremental migration and is suitable for transitioning from a Next.js app to a remix stack. Refactoring may be required for feature-specific and Next.js-coupled components, but the process is simplified because the features have already been moved out. Configuring the Monorepo to reference packages locally and linking them to the Next.js application is necessary. Nx provides benefits like fast refreshing, pre-configured setups, and features like local and remote caching.