<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Asibul Hasan</title>
    <description>The latest articles on DEV Community by Asibul Hasan (@asibul_hasan_5fe57cd945b8).</description>
    <link>https://dev.to/asibul_hasan_5fe57cd945b8</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3715877%2F546d528a-b3ad-437c-a46d-8f24fbe6cd29.jpg</url>
      <title>DEV Community: Asibul Hasan</title>
      <link>https://dev.to/asibul_hasan_5fe57cd945b8</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/asibul_hasan_5fe57cd945b8"/>
    <language>en</language>
    <item>
      <title>Setup MonoRepo in Nest.js</title>
      <dc:creator>Asibul Hasan</dc:creator>
      <pubDate>Sat, 17 Jan 2026 04:14:29 +0000</pubDate>
      <link>https://dev.to/asibul_hasan_5fe57cd945b8/setup-monorepo-in-nestjs-dj4</link>
      <guid>https://dev.to/asibul_hasan_5fe57cd945b8/setup-monorepo-in-nestjs-dj4</guid>
      <description>&lt;p&gt;Monorepos are becoming the default choice for backend teams that manage more than one service or shared library. Nest.js works very well in a monorepo setup, and it even has built-in support for it.&lt;/p&gt;

&lt;p&gt;In this post, you’ll learn:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What a monorepo is and why it’s useful&lt;/li&gt;
&lt;li&gt;How Nest.js supports monorepos&lt;/li&gt;
&lt;li&gt;How to set up a Nest.js monorepo step by step&lt;/li&gt;
&lt;li&gt;A clean folder structure that scales&lt;/li&gt;
&lt;li&gt;Common tips and pitfalls&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No fluff. Just what you need to get it working.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is a Monorepo?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;monorepo&lt;/strong&gt; is a single repository that contains multiple applications and shared libraries.&lt;/p&gt;

&lt;p&gt;Instead of this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;api-repo/
auth-repo/
shared-utils-repo/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You have this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;backend/
 ├─ apps/
 └─ libs/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why teams use monorepos
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Shared code lives in one place&lt;/li&gt;
&lt;li&gt;Easier refactoring across projects&lt;/li&gt;
&lt;li&gt;One set of tooling and configs&lt;/li&gt;
&lt;li&gt;Simpler dependency management&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For Nest.js projects with microservices, background workers, or shared modules, a monorepo is a natural fit.&lt;/p&gt;




&lt;h2&gt;
  
  
  Nest.js Monorepo Support (Built-In)
&lt;/h2&gt;

&lt;p&gt;Nest.js comes with &lt;strong&gt;first-class monorepo support&lt;/strong&gt; via the Nest CLI. You don’t need Nx or Turborepo to get started. The CLI can generate apps and libraries inside one workspace.&lt;/p&gt;

&lt;p&gt;This approach is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simple&lt;/li&gt;
&lt;li&gt;Official&lt;/li&gt;
&lt;li&gt;Easy to maintain&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Monorepo Folder Structure
&lt;/h2&gt;

&lt;p&gt;Here’s the structure we’re going to build:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nest-monorepo/
├─ apps/
│  ├─ api/
│  └─ auth/
├─ libs/
│  ├─ common/
│  └─ database/
├─ nest-cli.json
├─ tsconfig.base.json
└─ package.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Meaning:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;apps/&lt;/strong&gt; → runnable Nest.js applications&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;libs/&lt;/strong&gt; → shared modules, services, DTOs, helpers&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;common&lt;/strong&gt; → filters, guards, interceptors&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;database&lt;/strong&gt; → Prisma, TypeORM, or DB logic&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Image: Nest.js Monorepo Architecture
&lt;/h2&gt;

&lt;p&gt;You can include this image under the intro section.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="p"&gt;![&lt;/span&gt;&lt;span class="nv"&gt;Nest.js Monorepo Architecture&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="sx"&gt;https://your-image-url-here.com/nestjs-monorepo.png&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Image idea:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One repository box&lt;/li&gt;
&lt;li&gt;Inside it: &lt;code&gt;apps/api&lt;/code&gt;, &lt;code&gt;apps/auth&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Shared &lt;code&gt;libs/common&lt;/code&gt; and &lt;code&gt;libs/database&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Arrows showing shared usage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This visual helps readers understand the layout instantly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1: Create a New Nest.js Workspace
&lt;/h2&gt;

&lt;p&gt;Make sure you have the Nest CLI installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i &lt;span class="nt"&gt;-g&lt;/span&gt; @nestjs/cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a monorepo workspace:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nest new nest-monorepo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;During setup:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Choose &lt;strong&gt;npm&lt;/strong&gt;, &lt;strong&gt;yarn&lt;/strong&gt;, or &lt;strong&gt;pnpm&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Enable ESLint and Prettier if you want&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nest automatically creates a workspace-ready project.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: Enable Monorepo Mode
&lt;/h2&gt;

&lt;p&gt;Open &lt;code&gt;nest-cli.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"monorepo"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"root"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"apps/api"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"sourceRoot"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"apps/api/src"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"projects"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Setting &lt;code&gt;"monorepo": true&lt;/code&gt; tells Nest this repo will contain multiple apps and libraries.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3: Create Your First App
&lt;/h2&gt;

&lt;p&gt;Generate an app inside the monorepo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nest generate app api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create another one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nest generate app auth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your &lt;code&gt;apps/&lt;/code&gt; folder looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apps/
├─ api/
└─ auth/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each app is a fully independent Nest.js application.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 4: Create Shared Libraries
&lt;/h2&gt;

&lt;p&gt;Libraries are where monorepos shine.&lt;/p&gt;

&lt;p&gt;Create a shared library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nest generate library common
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a database library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nest generate library database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;libs/
├─ common/
└─ database/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each library has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Its own module&lt;/li&gt;
&lt;li&gt;Its own &lt;code&gt;tsconfig&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Clean import paths&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 5: Import a Library Into an App
&lt;/h2&gt;

&lt;p&gt;Example: use &lt;code&gt;CommonModule&lt;/code&gt; in the API app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// apps/api/src/app.module.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;CommonModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@app/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;CommonModule&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;@app/*&lt;/code&gt; alias is auto-configured by Nest.&lt;/p&gt;

&lt;p&gt;No relative path mess. No hacks.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 6: Run Apps Independently
&lt;/h2&gt;

&lt;p&gt;Run the API app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nest start api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the auth app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nest start auth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each app:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Has its own port&lt;/li&gt;
&lt;li&gt;Has its own environment variables&lt;/li&gt;
&lt;li&gt;Can be deployed separately&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 7: Build for Production
&lt;/h2&gt;

&lt;p&gt;Build a specific app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nest build api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Build all apps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nest build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output goes to the &lt;code&gt;dist/&lt;/code&gt; folder, separated by app name.&lt;/p&gt;




&lt;h2&gt;
  
  
  Best Practices for Nest.js Monorepos
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Keep libraries small
&lt;/h3&gt;

&lt;p&gt;Libraries should solve one problem. Don’t dump everything into &lt;code&gt;common&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Avoid circular dependencies
&lt;/h3&gt;

&lt;p&gt;If two apps depend on each other, rethink the design.&lt;/p&gt;

&lt;h3&gt;
  
  
  Share DTOs and interfaces
&lt;/h3&gt;

&lt;p&gt;This avoids duplication and keeps APIs consistent.&lt;/p&gt;

&lt;h3&gt;
  
  
  One database library
&lt;/h3&gt;

&lt;p&gt;Centralize database logic instead of duplicating connections.&lt;/p&gt;




&lt;h2&gt;
  
  
  When NOT to Use a Monorepo
&lt;/h2&gt;

&lt;p&gt;A monorepo might not be ideal if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have a single small service&lt;/li&gt;
&lt;li&gt;Teams are fully independent&lt;/li&gt;
&lt;li&gt;Deployment pipelines are very different&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For most growing backend systems, though, it’s worth it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Nest.js monorepos are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Easy to set up&lt;/li&gt;
&lt;li&gt;Officially supported&lt;/li&gt;
&lt;li&gt;Great for scaling backend systems&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You get clean boundaries, shared code, and fewer repositories to manage. Start simple, add apps and libraries as your system grows, and keep the structure boring and predictable.&lt;/p&gt;

&lt;p&gt;That’s what scales.&lt;/p&gt;




&lt;p&gt;If you want, I can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add &lt;strong&gt;Nx&lt;/strong&gt; on top of this&lt;/li&gt;
&lt;li&gt;Convert it to a &lt;strong&gt;Medium-style blog&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Create a &lt;strong&gt;real architecture diagram image&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Add &lt;strong&gt;Docker + monorepo deployment&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just tell me.&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>angular</category>
      <category>monorepo</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
