{"post":{"id":"81","slug":"nx-nestjs-backend-part-3","title":"Nx Monorepo Essentials: Integrating a NestJS Backend (Part 3/3)","excerpt":"Complete your full-stack monorepo by integrating a NestJS backend. Learn how to set up, share types, and connect your frontend and backend seamlessly with Nx.","content":"\r\n# Nx Monorepo Essentials: Integrating a NestJS Backend (Part 3/3)\r\n\r\n> **By Sidharrth Mahadevan** | Published: February 23, 2025\r\n> [Original Article on Medium](https://medium.com/@sidharrthnix/nestjs-backend-with-nx-setting-up-your-server-in-a-monorepo-33f5646f4fcc)\r\n\r\n---\r\n\r\n## Introduction\r\n\r\nWelcome to the **final installment** of our Nx Monorepo Series! If you've been following along:\r\n\r\n- In **Part 1**, we set up our Nx workspace and laid the foundation.\r\n- In **Part 2**, we built a sleek Next.js 15 frontend with the App Router.\r\n- Now, in **Part 3**, it's time to give our application a robust server-side foundation with **NestJS** — all within the same Nx monorepo.\r\n\r\nIf you thought the frontend was cool, wait until you see how we set up our backend!\r\n\r\n> **Fun Fact:** A well-structured backend is like a well-brewed cup of coffee — strong, reliable, and absolutely essential.\r\n\r\n---\r\n\r\n## Series Overview\r\n\r\n- **Part 1:** Nx Monorepo Essentials: Laying the Foundations\r\n- **Part 2:** Nx Monorepo Essentials: Crafting a Next.js 15 Frontend\r\n- **Part 3:** Nx Monorepo Essentials: Integrating a NestJS Backend *(You're here!)*\r\n\r\n---\r\n\r\n## Adding NestJS to Your Nx Workspace\r\n\r\nFirst, add the NestJS Nx plugin:\r\n\r\n```bash\r\nnx add @nx/nest\r\n```\r\n\r\nThen generate a new NestJS application:\r\n\r\n```bash\r\nnx g @nx/nest:app server\r\n```\r\n\r\nYour monorepo will now look something like this:\r\n\r\n```\r\nmy-workspace/\r\n├── apps/\r\n│   ├── client/          # Next.js 15 frontend\r\n│   └── server/          # NestJS backend\r\n├── libs/                # Shared libraries\r\n├── nx.json\r\n└── package.json\r\n```\r\n\r\n---\r\n\r\n## Generated NestJS App Structure\r\n\r\n```\r\nserver/\r\n├── src/\r\n│   ├── app/\r\n│   │   ├── app.controller.ts\r\n│   │   ├── app.controller.spec.ts\r\n│   │   ├── app.module.ts\r\n│   │   └── app.service.ts\r\n│   └── main.ts\r\n├── project.json\r\n└── tsconfig.json\r\n```\r\n\r\n---\r\n\r\n## Understanding `nx.json`\r\n\r\nAs you've seen in previous parts, `nx.json` orchestrates your entire monorepo. It configures inputs, plugins, and default targets for all your applications — both the Next.js frontend and the NestJS backend — from a single configuration file.\r\n\r\n```json\r\n{\r\n  \"targetDefaults\": {\r\n    \"build\": {\r\n      \"cache\": true\r\n    },\r\n    \"test\": {\r\n      \"cache\": true\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nThis single configuration file helps Nx seamlessly build, serve, test, and lint every piece of your monorepo.\r\n\r\n---\r\n\r\n## Running the NestJS Server\r\n\r\nThis command builds your NestJS app (if needed) and then starts the local server:\r\n\r\n```bash\r\nnx serve server\r\n```\r\n\r\nCheck it out at `http://localhost:4002/api` (by default).\r\n\r\n---\r\n\r\n## Building for Production\r\n\r\n```bash\r\nnx build server\r\n```\r\n\r\n---\r\n\r\n## Running Tests\r\n\r\n> Good tests are like double-checking your parachute before you jump. Always test first; you won't regret it!\r\n\r\n```bash\r\nnx test server\r\n```\r\n\r\n---\r\n\r\n## Linting\r\n\r\n```bash\r\nnx lint server\r\n```\r\n\r\n---\r\n\r\n## Sharing Code Between Frontend and Backend\r\n\r\nOne of the biggest wins in a monorepo is the ability to share types and utilities between your Next.js frontend and your NestJS backend. Create a shared library:\r\n\r\n```bash\r\nnx g @nx/js:lib shared-types\r\n```\r\n\r\nYou can now import the same interfaces and DTOs in both apps:\r\n\r\n```ts\r\n// In NestJS backend\r\nimport { CreateUserDto } from '@myorg/shared-types';\r\n\r\n// In Next.js frontend\r\nimport { CreateUserDto } from '@myorg/shared-types';\r\n```\r\n\r\n> **Important:** Use a `shared/` folder as a neutral zone for variables, helpers, and utilities that need to be accessed by both frontend and backend. This prevents circular dependency errors.\r\n\r\n---\r\n\r\n## Running Both Apps Together\r\n\r\nYou can run both applications simultaneously:\r\n\r\n```bash\r\n# In one terminal\r\nnx serve client\r\n\r\n# In another terminal\r\nnx serve server\r\n```\r\n\r\nOr run all projects in parallel:\r\n\r\n```bash\r\nnx run-many --target=serve --all\r\n```\r\n\r\n---\r\n\r\n## Full-Stack Summary\r\n\r\nHere's what your complete workspace looks like now:\r\n\r\n| Project  | Technology  | Default Port       |\r\n|----------|-------------|--------------------|\r\n| `client` | Next.js 15  | `http://localhost:4200` |\r\n| `server` | NestJS      | `http://localhost:4002/api` |\r\n\r\n---\r\n\r\n## What We've Built\r\n\r\nCongratulations — you now have a **full-stack Nx monorepo** with a Next.js 15 frontend and a NestJS backend living in perfect harmony!\r\n\r\n- **Part 1** introduced Nx and laid the foundation.\r\n- **Part 2** walked you through building and configuring the Next.js 15 app.\r\n- **Part 3** (this post) wrapped it up with a NestJS server to power all your backend needs.\r\n\r\n> **Final Thought:** A well-structured monorepo using Nx doesn't just make your code cleaner and your builds faster — it makes life as a developer downright enjoyable. Now, go forth and build something amazing with your new Nx superpowers!\r\n\r\n---\r\n\r\n*Thanks for reading! If you enjoyed this guide, be sure to share it with your fellow developers.*\r\n  ","category":"Tutorial","author":"Sidharrth Mahadevan","published_at":"2025-02-23T00:00:00.000Z","read_time":"12 min read","image_url":"https://miro.medium.com/v2/resize:fit:1100/format:webp/1*60fhtSFReTeVlHmD9U7tYA.png","tags":["Nx","Monorepo","NestJS","Backend","Fullstack"],"featured":false,"created_at":"2026-04-01T06:13:26.623Z","image_attribution":"Image by rawpixel.com on Freepik"}}