Nestjs project structure example. nest new my-nest-app cd my-nest-app.
Nestjs project structure example NestJS Basics: Modular by Design. NestJS - A progressive NodeJS framework for building efficient, reliable and scalable server-side applications. ts: The unit tests for the controller. A newly created NestJS project has the following structure: Custom Logger with nestjs-pino; Custom Decorators Example at Nest level; Configuration; Authentication - JWT and Session login with Passport; Role-based Guard; Controller Routes Auth Login; Sample Parameter and DTO; CRUD API Sample; Database Query Example; Unit Test; E2E Test; Shared Modules Example; GraphQL Structure Example Mar 29, 2023 · With Nest installed, we now have to plan our projects. Since we’re going to be building NestJS microservices, we’re not going to build one single project; instead, we want to build one project per microservice and one project per client. Install it globally using npm: npm install -g @nestjs/cli Step 2: Initialize NestJS Project . Modular Architecture with Modules and Services NestJS encourages a modular architecture to keep your codebase organized. js web applications quickly (reusing clean code), (iii 5 days ago · Example Project. You should choose the right architecture [1] (Layered, Clean, Onion, Hexagonal ) [2] based on the size of your project. @nestjs/cli - CLI tool for Nest. In our blog example, we had higher level user and auth modules. /app: The heart of your application. Run 'yarn dev' or 'npm run dev' in the 'nest-demo' project to start API Gateway. NestJS is a… Mar 1, 2025 · This command will prompt you to choose a package manager (npm or yarn). To create an event handler, you can use the @EventPattern() decorator, which is imported from the @nestjs/microservices package. There are 5 files: app. Jul 25, 2020 · We found that DDD outlines a clear structure to your code that not only makes sense but can minimise the amount of code/methods/classes that resides in aspects of your domain. Implement function to bundle the workflows: Nov 18, 2024 · Project Structure Here’s an example structure for a NestJS module that follows the layered architecture principles: This modular setup ensures clear separation of concerns, making the A full-stack TypeScript solution, and starter project. These are the core Jul 11, 2024 · To create a new NestJS project, you can use the NestJS CLI (@nestjs/cli). - chnirt/nestjs-graphql-best-practice Jun 19, 2023 · Now, you can cd into the newly created project, and open it with an editor of your choice: $ cd getting-started-with-nestjs. spec. Here’s an image of my project structure. Mar 17, 2024 · NestJS CLI; Step 1: Setting Up the Project Structure. But it does provide several features to help you organize your project. Run 'yarn dev' or 'npm run dev' in the 'nest-microservice' project to start microservice. NestJS encourages a well-organized project structure that makes it easy to manage and maintain your code. You will be prompted to choose a package manager. Contribute to c-conklin/sample-nestjs-project-structure development by creating an account on GitHub. Article: Onion Architecture Explained by Jeffrey Palermo. This command will generate a new NestJS project structure in the “nest-example” directory. node_modules npm ci # 2. A newly created NestJS project has the following structure: We would like to show you a description here but the site won’t allow us. nest new project-name . Instead, you should be using platform: or framework: tags (usually, the more specific you can be, the better, within reason), as specified in the Angular Enterprise MonoRepo Patterns book Sep 23, 2021 · Run PostgreSQL container by using 'docker-compose up' in 'nest-microservice' project or by using Docker Desktop. . When you’re ready, let’s initialize a new Nest project. To set up a NestJS project, you’ll need to first install the NestJS CLI tool, like so: npm i -g @nestjs/cli Dec 8, 2022 · Project Structure. js. Topics Example CRUD: create, read, update and delete users! Authentication with JWT tokens; More logical directory structure; Examples of NestJS controllers, services, repositories, guards, interceptors, etc This project is an implementation of the well-known ddd-by-examples/library, but this time using TypeScript and Node. module. Read about microservice frameworks here Aug 14, 2024 · Node. Each folder represents a route segment that is mapped Node. This repository provides a comprehensive example of how to structure a NestJS application, including best practices for modularity, dependency injection, and testing. Before we begin, keep in Nov 15, 2024 · Official NestJS Documentation. main. Jul 19, 2024 · It is the best practice to make a Project structure for Angular project, separate files and folders for difference purpose helps in keeping the files aligned and making the work of end user easy. js Project To begin, install Nest. Includes an API, CLI, and example client webapp. @nestjs/typeorm - TypeORM module for Nest. Given we’re going to build one single microservice here, let’s build two projects in total: Jul 14, 2024 · This will create a new directory my-nest-app and set up a basic NestJS project structure inside it. Modular Structure. This article will discuss 10 best practices for structuring your NestJS project folders. Nov 21, 2024 · For a backend framework like NestJS, which thrives on modularity and clarity, naming conventions are especially important. Run the following command to set up a new NestJS project using the Nest CLI: $ npx @nestjs/cli new nest-example. Mar 8, 2023 · To demonstrate the Clean Architecture pattern in action, we will use a TypeScript project example . Jul 18, 2024 · main. js framework NestJS project structure. ts. Below is an example of a We would like to show you a description here but the site won’t allow us. Reusability: Dynamic modules can be reused across different parts of your application with different configurations. content_copy Mar 8, 2020 · Let's first define what a monorepo is. ts: The entry point of the application. js and Typescript Step 1: Let’s create Task Module in our application using the Nest CLI command $ nest generate module Task. To see a full example of a dynamic module in action, you can refer to the NestJS example project. For example, if you want to notify another service that a specific condition has occurred in this part of the system, the event-based message style is ideal. NestJs , Redis , Prisma , Docker , Docker Compose , Best Practice - mewpk/NestJs-Project-Structure-Best-Practices Node. 5 days ago · Once the CLI is installed, you can create a new NestJS project by running: nest new project-name This command will prompt you to choose a package manager (npm or yarn) and then set up a new project with a default structure. First, we’ll install Nest CLI. Let's take a closer look at the key directories and their purposes: /src: This is where your application code resides. Jun 17, 2023 · Project Setup Start by creating a new directory for your NestJS project. js project to improve scalability, maintainability, and readability with a… Scaffold quickly your next NestJS 10 API project with ️ using this template. ts: Defines the application's routes and request handling logic. We use techniques strongly connected with Domain Driven Design, Behavior-Driven Development, Event Storming Jun 15, 2022 · Creating a new Nest project for our ecommerce app. Passport Email Auth - Starter project that includes API for user email authentication with MongoDB and PassportJs. Most of our codes will be written in the src folder. Mar 29, 2023 · With Nest installed, we now have to plan our projects. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming). nest generate module May 21, 2019 · After reading documentation and try create nestjs project, I'm confused with project structure. In this lesson, we’ll delve into the NestJS project structure before discussing the core concepts of the Dec 9, 2022 · When building a NestJS application, it is important to structure the code in a way that is easy to maintain and understand. cd apps/nest # Run any nest-cli commands in here ex: nest g m <module-name> Shared (Typescript) cd libs/shared # Creae any number of codes that you want to share and make sure you export the files in index. It is highly extensible because of its structure and IoC mechanism which allows plugging any library in a very convenient way. Currently on my backend have Controller, Model, Event, Middleware, Validator, interface, static file server (subdomain Aug 23, 2023 · This will create a basic project structure with a default app module. Alternatives. After selecting your preferred package manager, the CLI will set up a new NestJS project in a directory named project-name. Step 2: Creating a Basic Module, Controller, and Service May 26, 2024 · Initially, the project had a smaller scope, but it has since expanded significantly, qualifying it as a large-scale endeavor. js Project Structure: Best Practices and Example for Clean Code Discover the top best practices for organizing a Node. The producerOnlyMode option will bypass consumer group registration for the api-gateway app and only function as a producer. 1. By following these best practices, you can ensure that your application is organized and easy to navigate. Building a hexagonal architecture (port-adapter) services in nestjs, this project is open source, with explaination and example. Open the terminal and run the following command: npm i -g @nestjs/cli Once the installation is complete, create a project folder: mkdir VideoStreamApp && cd VideoStreamApp Next, create the new NestJS project by running this command: nest new backend Aug 23, 2023 · In essence, NestJS Folder Structure Best Practices refer to a set of recommended guidelines and patterns for arranging the various components, modules, controllers, services, and other pieces of your NestJS application within directories. NestJS generates a basic folder structure with some files pre-configured. This temporal-worker is inside apps. js Response Project structure. app. js project to improve scalability, maintainability, and readability with a… Feb 3, 2022 · For example, (i) how to use a running example as a base to learn Nest. js server-side applications. For me the generated NestJS project from the CLI is a perfect starter. In the app directory, nested folders define route structure. Explore this online nestjs-project-structure sandbox and experiment with it yourself using our interactive online playground. Actions needed: 1. ts: The root module of the application. Contribute to CatsMiaow/nestjs-project-structure development by creating an account on GitHub. Features include production grade logging, authorization, authentication, MongoDB migrations, and end-to-end testing. One of the initial considerations when starting a project is where to place files and how they will relate to each other. Navigate to the directory where you want to create Introduction. Apr 12, 2022 · For example, in the above schema, we have a total of 5 properties like name, roleNumber, Project Structure. ts to export the functions to be bundled. For simplicity, let’s name the microservices as follows: auth-service; blog-service; projection-service; mkdir cqrs-example cd cqrs-example nest new auth-service nest new blog-service nest new projection Dec 31, 2023 · Exploring the project structure. NestJS organizes your backend into modules, which serve as feature-specific building blocks. nest new my-nest-app cd my-nest-app. Now, as with most applications, the user tends to take NestJS, Prisma and GraphQL codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the RealWorld spec and API. However, it’s essential to start with a solid foundation that aligns with best practices and real-world development scenarios. ; src NestJS (Express + TypeORM + GraphQL + MongoDB) codebase containing real world examples (CRUD, auth, advanced patterns, etc). Feb 7, 2022 · To install and configure a new NestJS project, we’ll use Nest’s command-line interface. where we should put the auth module (in common or in its own module) 2. You can use it as a template to jumpstart your development with this pre-built solution. nest generate module users nest generate controller users nest generate service users Project Structure: Project Structure Step 3: Install Dependencies npm install class-validator class About. This is the most basic structure to start a NestJS project. js Request | Param, Body, Query, Headers, IP 6 Redirection in Nest. In a modular structure, the application is divided into separate modules, each with its own controller, service, and other related files. Step 2: Create a new project. Crafted for Docker environments (Dockerfile support and environment variables) REST API with Prisma support; Swagger documentation, Joi validation, Winston logger, Folder structure, code samples and best practices; Fast HTTP server with Fastify Sep 12, 2024 · Run the following command to create a new NestJS project: npm i -g @nestjs/cli nest new task-manager-backend. Nov 15, 2024 · Project Structure Overview In this guide, each module is structured based on Onion Architecture principles, with specific directories for each layer: Domain , Application , and Infrastructure . js 2 Understanding Default NestJS Project Structure 3 more parts 3 Nest. If you focus on the performance or features of the module, you can consider: Fastify instead of Express; Prisma or Sequelize instead of TypeORM; SWC instead of TypeScript compiler; Vitest instead of Jest Nov 8, 2024 · Group related endpoints into modules for better structure and reusability. Just, why no one is using it? People seem to prefer custom top-level folders, Clean architecture, DDD, random whatever structure, but why? I know Nest. For Example: Components to be in Components folder, Services to be in Services folder, etc. For example, create separate modules for different functional domains: Example Project Structure. This seemingly minor detail plays a pivotal role in ensuring codebase clarity, scalability, and ease of Oct 9, 2024 · Understanding the Folder Structure. Mar 25, 2025 · To see a practical implementation, you can check out the NestJS project example on GitHub. 🚀 Want more details or visual examples? Leave Serverless NestJS TypeOrm - Example how to NestJS using the serverless framework with TypeORM. With the project now created, we can run it with either of the following commands: $ npm run start // Or $ yarn start // Or $ pnpm run start Sep 24, 2021 · NestJS is an Angular-inspired framework that provides strongly determined project structure, common design patterns, and best approaches. Started from this issue: nestjs/nest#2249. After that create new project by running nest new <project name>. This project demonstrates how to use dynamic modules effectively in a real-world application. test/: Contains test files. Nov 14, 2022 · For example, consumers with groupId auth-consumer can only read events published with groupId: 'auth-consumer'. The workflows folder contains all the workflows in my application and an index. Oct 25, 2023 · Now, let's do a quick overview of the project folder structure. Demo This codebase was created to demonstrate a fully fledged fullstack application built with NestJS, Prisma and GraphQL including CRUD operations, authentication, routing, pagination, and more. we will call our project as blog-api. js framework designed to build scalable and efficient server-side applications. js Controller in Easier Terms 4 Nest. js 7 HTTP Status Code and HTTP Header in Nest. He is the author of a few books where you can understand his concept. NestJS, unlike other frameworks, offers flexibility in how we structure our projects. Once you scaffold a new NestJS project using its CLI, it gives you a few boilerplate files to start with. Martin. 5. Jul 18, 2024 · Here’s an overview of a typical NestJS folder structure: NestJS Folder Structure Steps to Create NestJS Application. js pages ├── server # nest. Creating a New NestJS Project using the below command. When import from an existing database npm run entity # 2-2. Colocation. Now we start the NestJS application by using the command npm run start, Nov 18, 2024 · Project Structure Here’s an example structure for a NestJS module that follows the layered architecture principles: This modular setup ensures clear separation of concerns, making the A full-stack TypeScript solution, and starter project. js app is structured as follows: Organizing your project. Example of by default nest js project structure 2. Apart from folder and file conventions, Next. To kick off things even faster NestJS comes with a nice CLI tool that handles the boilerplate for us. js is following a component structure, but apart from it, I see a different tech-role-based set of dirs on every project. ts: A basic controller with a single route. Step 4: Generate a new module. a monorepo (a syllabic abbreviation of a monolithic repository) is a software development strategy where code for many projects is stored in the same repository. A typical NestJS project structure includes the following directories and files: src/: Contains the main application code. Here’s a quick overview: src/app. JS Project Structure node_modules - Contains all the libraries that are required for your project. js project to improve scalability, maintainability, and readability with a… Sep 3, 2023 · Additional Note: After helping someone in the Nest Discord server once more about project structure, I realized one more advantage to Nest's modules. Mar 25, 2025 · Project Structure. Jan 5, 2022 · I also recommend using the following directory structure that resembles a monorepo └── src ├── client # client code: hooks, components, etc ├── pages # actual NEXT. json in <app_name> Nest is a framework for building efficient, scalable Node. ts that contained ALL logic for the user. It uses progressive JavaScript, is built with and fully supports TypeScript (yet still enables developers to code in pure JavaScript) and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming). Its modular architecture, coupled with TypeScript, makes it a favorite for developers looking to streamline backend development. Navigate to the desired location and run #1. Here's an example of creating a module and service: Aug 9, 2023 · Step 1: Create a New Nest. Create a new NestJS project with the necessary folder structure for each microservice. JS has generated a starter code. Inspect the generated project structure, noting the src directory which contains your application’s source code, along with entry file main. Given we’re going to build one single microservice here, let’s build two projects in total: Jul 9, 2021 · Clean architecture was created by Robert C. service. Together, they can create a complete web application with a robust backend and a dynamic, interactive frontend. NestJS Project Directory structure. Open a terminal and navigate to the desired location. npm install -g @nestjs/cli . Official Documentation. Jan 21, 2021 · Creating a new project First we need to install nest-cli using npm i -g @nestjs/cli. npm i -g @nestjs/cli nest new nestjs-dto-interface-example cd nestjs-dto-interface-example. Benefits. Now we start the NestJS application by using the command npm run start, Open source headless CMS API written using NestJS, that has pre built modules like User Management, Role Management, Permission Management, Email Module, Account Settings, OTP, Throttling, RBAC support, Localization, and many more. @nestjs/config - Configuration module for Nest. In my opinion generated project structure not scalable, it strange when large codebase have project structure like that. Sep 3, 2023 · Additional Note: After helping someone in the Nest Discord server once more about project structure, I realized one more advantage to Nest's modules. The ‘src’ folder typically houses the controllers, providers (services), modules, and other TypeScript files. 4. This is a project of a library, driven by real business requirements. This project uses a Monorepo structure and is managed using Rush. Here is a typical example: Aug 31, 2023 · Node. react javascript graphql internal-tools crud admin typescript nextjs headless developer-tools admin-ui react-framework frontend-framework open-source-project hacktoberfest ant-design low-code nestjs good-first-issue react-hooks Nov 24, 2022 · Understanding Nest. Features and Nest is a framework for building efficient, scalable Node. database model entity generate # 2-1. When working with a NestJS project, it's important to follow a well-organized structure. js Nested Routing, Wildcard, Request 5 Nest. Presently, the architecture of my Nest. NestJS Project Structure - Example of constructing a project structure with NestJS. In effect though, they could be considered "things I would always need in any app". js server code └── shared # common types, utils, constants etc IMO build some API first using plain NodeJs + express this will make you learn more than just starting Nestjs because Nest has a lot of "Covering" and under the hood stuff that you wouldn't know how they were built unless you do some plain work first Jan 1, 2024 · Create a new NestJS project: nest new nestjs-mongodb-crud Create a schema that reflects the structure of the data in MongoDB. Setting up the NestJS project. cd project-name . also where we should put users module ( because it may be used by other modules) I am bit confused about this what should be sharable in any module This will also flatten your structure greatly and make your MonoRepo more navigable, manageable and understandable, by reducing your project name lengths. controller. Choose your package manager (npm or yarn), and the CLI will scaffold the project for A working example of microservice architecture implementation using nestjs framework - Denrox/nestjs-microservices-example Sep 30, 2020 · This structure is nice and I wanted to go for something like this especially the common module but I am confused about some things such as (they may be dumb) 1. Example: Inside the task folder you will find a similar root module folder structure. ts; Add other frameworks. js, (ii) take advantage of our public book repository to create Nest. Step 2: Generate User Module. js by Khalil Stemmler. The core sub-project contains the core business logic, while koa and nestjs-app use different Aug 30, 2020 · NestJS. Sep 28, 2023 · NestJS Project Structure. @nestjs/jwt - Utilities module based on the JWT package. Then, we will create a new project: npm install -g @nestjs/cli nest new nestjs-ecommerce After installation is complete, navigate to the project and start it: cd nestjs-ecommerce npm run start:dev 83k stars, "structure your solution by components" is #1 at the very top. src - Contains your application source code, Under this directory Nest. Feb 27, 2023 · Node. When build only the entity folder npm run build:entity Jul 8, 2024 · Best Practices in NestJS Folder Structure. Jul 25, 2024 · Project Structure: Steps to Build web API using Nest. Project Structure. Modules encapsulate related components, and services handle business logic. - ablestack/nestjs-bff Dec 10, 2024 · NestJS is a progressive Node. Steo 3: Navigate to the project directory. @nestjs/swagger - OpenAPI (Swagger) module for Nest. Let's take a look at the src folder. To use our old server as an example; we had a service called user. Tutorial: Using DDD with Node. Jul 16, 2020 · I’ve been building apps using NestJS for quite a while now and I want to share the best way to structure your directory/code with other developers new to this framework. Step 1: Install NestJS CLI. For more detailed information, visit the official NestJS NestJS Project Structure Example. May 14, 2021 · For example an ODM like Mongoose. Node. Here are some key concepts to follow for standardizing folder organization: 1. A well-organized folder structure is crucial for maintaining and scaling an application. But in our case, we will keep the focus on Clean Architecture. I Feb 14, 2024 · Both frameworks use decorators, TypeScript, and follow a consistent structure, making it easier for developers familiar with Angular to transition seamlessly to server-side development with NestJS. Angular (Example) cd apps; ng new <app_name> Remove node_modules and package-lock. A module contains everything a feature needs: We found that DDD outlines a clear structure to your code that not only makes sense but can minimise the amount of code/methods/classes that resides in aspects of your domain. Oct 8, 2019 · Project and tooling. js CLI and create a new project: npm install -g @nestjs/cli nest new microservices-project cd microservices-project May 9, 2023 · Now that we’ve covered what NestJS is and its main benefits and use cases, it’s time to talk about creating your first NestJS project. Nest (NestJS) is a framework for building efficient, scalable Node. Running the application Dec 25, 2023 · 1 Getting Started With Nest. The server folder contains three sub-projects: core, koa, and nestjs-app. Now, as with most applications, the user tends to take Node. js is unopinionated about how you organize and colocate your project files. A typical NestJS Feb 23, 2023 · NestJS and Angular complement each other well in full-stack web development. ts: This is the entry point of your application where the NestJS application is bootstrapped. Oct 16, 2024 · Step 1: Initialize a NestJS Project. hkhnxpyrmlnoxxoqcyodtbcpuabfqkpfiqjxyfiwfdmxvdxgveeewgnfiutnuthyhofpkkym