Folder Structure of a NestJS Project
Last Updated :
18 Jul, 2024
Organizing a NestJS project with a well-planned folder structure is important for readability, scalability, and maintainability. A clear structure helps in managing code, configurations, modules, and other assets effectively. In this article, we are going to learn the folder structure of a NestJS project and explore the best practices for organizing it, including flat structure, modular structure, and Domain-Driven Design (DDD).
Prerequisites
To understand the folder structure for a NestJS project, ensure you have:
Folder Structure Overview
The folder structure of a NestJS application contains various files and directories that are essential for organizing the project's components, services, modules, and configurations. Here’s an overview of a typical NestJS folder structure:
NestJS Folder StructureSteps to Create NestJS Application
Step 1: Install NestJS CLI
npm install -g @nestjs/cli
Step 2: Create a new project
nest new project-name
Steo 3: Navigate to the project directory
cd project-name
Step 4: Generate a new module
nest generate module module-name
Step 5: Generate a new controller
nest generate controller controller-name
Step 6: Generate a new service
nest generate service service-name
Step 7: Create Common Directories and Files
Create directories like common
, config
, modules
, shared
, utils
, and assets
to organize your code logically.
cd src
mkdir common
mkdir config
mkdir modules
mkdir shared
mkdir utils
mkdir assets
Now, the folder structure will look like this
Improved NestJS Folder StructureImproved Files and Folders Structure
For managing the project in a more concise way, we can create these folders for better code organization. Here's a detailed breakdown of each folder and its purpose:
1. Common Folder: The common
folder is used to store components that are shared across multiple modules and are fundamental to the application's structure. This can include:
- Decorators: Custom decorators for routes, parameters, or other features.
- Filters: Global or specific filters for handling exceptions and errors.
- Guards: Authorization guards to protect routes based on roles or permissions.
- Interceptors: Interceptors for logging, transformation, or other middleware-like tasks.
- Pipes: Pipes for transforming or validating incoming data.
2. Config Folder: The config
folder is used for storing configuration files that manage different environments and settings. This includes:
- Environment Configurations: Separate files for development, production, and testing environments.
- App Configuration: General application settings, like database connections, API keys, and other environment variables.
3. Modules Folder: The modules
folder is the core of your application, containing feature-specific modules. Each module encapsulates related controllers, services, DTOs, and entities, providing a clear separation of concerns and promoting modular design.
4. Shared Folder: The shared
folder includes modules or services that are used across multiple features of the application. This ensures reusability and avoids duplication of common functionalities.
5. Utils Folder: The utils
folder is used for utility functions and helper methods that can be utilized throughout the application. These functions typically include common operations like data formatting, calculations, or other reusable logic.
6. Assets Folder: The assets
folder stores static assets like images, icons, fonts, and other media files. Organizing these assets in a dedicated directory keeps your project clean and makes it easy to locate and manage these files.
7. Styles Folder: The styles
folder contains CSS or other styling files used to define the visual appearance of your application. This folder helps in maintaining a consistent style across the application and makes it easier to manage styles for different components and modules.
Folder Structure based on Project Size
NestJS applications have a well-defined and intuitive folder structure that helps in organizing code efficiently. There are different approaches to structuring a NestJS application based on the size and complexity of the project:
- Flat Structure: Suitable for small projects with fewer modules.
- Modular Structure: Recommended for medium to large projects, where each module has its own folder.
- Domain-Driven Design (DDD): For very large applications, where the structure is based on business domains.
1. Flat Structure
In a flat structure, all files are placed in a single directory or a few directories without much nesting. This approach is simple but can become unmanageable as the project grows.
Components:
- app.controller.ts: This file contains the main controller for your application, handling incoming requests and returning responses.
- app.module.ts: This is the root module of your application where you import other modules.
- app.service.ts: This file contains the main service logic for your application.
- main.ts: This is the entry point of your application where the NestJS application is bootstrapped.
Example of by default nest js project structure2. Modular Structure
In a modular structure, the application is divided into separate modules, each with its own controller, service, and other related files. This structure is scalable and helps in organizing code better.
Components:
- common/: This folder contains reusable components like filters and guards.
- modules/: This folder contains different feature modules of the application.
- users/: This folder contains files related to the users module.
- users.controller.ts: Handles incoming requests for the users module.
- users.module.ts: Defines the users module and imports necessary dependencies.
- users.service.ts: Contains business logic related to users.
- products/: This folder contains files related to the products module.
- products.controller.ts: Handles incoming requests for the products module.
- products.module.ts: Defines the products module and imports necessary dependencies.
- products.service.ts: Contains business logic related to products.
Modular Structure3. Domain-Driven Design (DDD)
In a DDD structure, the application is organized around business domains. Each domain contains its own modules, controllers, services, and other related files. This approach is suitable for very large and complex applications.
Components:
- domain/: This folder is organized by business domains.
- user/: Contains files related to the user domain.
- application/: Contains application logic.
- dto/: Data Transfer Objects for user-related data.
- service/: Application services for user domain.
- domain/: Contains domain logic.
- entities/: Domain entities for user.
- repositories/: Domain repositories for user.
- infrastructure/: Contains infrastructure logic.
- controllers/: Controllers for user domain.
- repositories/: Infrastructure repositories for user domain.
- product/: Contains files related to the product domain.
- application/: Contains application logic.
- domain/: Contains domain logic.
- infrastructure/: Contains infrastructure logic.
Example
Similar Reads
Folder structure for a Node JS project
Organizing a Node JS project with well planned folder structure is crucial for readability, scalability, and maintainability. A clear structure helps in managing code, configurations, modules, and other assets effectively. In this article, we are going to learn the folder structure of the Node JS pr
5 min read
Folder Structure of Angular Project
Creation and Organization of an Angular Project in a well-planned manner is very important for readability, scalability, and manageability. A clear structure helps in managing code, configurations, modules and services effectively. In this article, we are going to have a clear understanding of the f
4 min read
Folder Structure for a React JS Project
Organizing a React project with a well-planned folder structure is very important for readability, scalability, and maintainability. A clear structure helps in managing code, configurations, modules, and other assets effectively. In this article, we are going to learn the folder structure of the Rea
5 min read
NextJS 14 Folder Structure
Next.js, a powerful React framework developed by Vercel, continues to evolve, bringing new features and improvements with each release. Version 14 of Next.js introduces enhancements to the folder structure, making it more efficient for developers to organize their projects. In this article, weâll ex
4 min read
NuxtJS Directory Structure
In this article, we are going to learn about the directory structure of NuxtJs. Nuxt.js is a free and open-source web application framework based on Vue.js, Node.js, Webpack, and Babel.js. Nuxt is inspired by Next.js, which is a framework of similar purpose, based on React.js.Create NuxtJS Applicati
3 min read
Express.js app.router Property
The Express.js app.router property was introduced in Express 4. Â It helps us to create modular, mountable route handlers. It provides us with many features such as it extends this routing to handle validation, handle 404 or other errors, etc. It helps us to organize our file structure for our server
3 min read
Best ways to Structure React Components
Structuring your React applications is critical for ensuring maintainability, scalability, and code readability as a software developer. In this tutorial, we'll delve into the best practices for organizing a React app, offering examples and code snippets along the way. Make sure you have the latest
3 min read
MERN Stack Project SetUp - A Complete Guide
Setting up a MERN Project involves several crucial steps to ensure functionality, security, and user-friendliness. Whether you're launching a new venture or expanding an existing business online, a well-structured project setup is key to success. Hereâs a comprehensive guide on how to effectively se
3 min read
Build a Project Management Tool Using NextJS
A project management tool is very crucial for organizing tasks which helps us to be productive. In this article, we will guide you through the process of building a simple project management tool using Next.js, a powerful React framework that allows you to build server-rendered React applications wi
6 min read
Top 7 Node.js Project Ideas For Beginners
Node.js, a powerful JavaScript runtime built on Chrome's V8 engine, has become a cornerstone for developing scalable network applications. Its non-blocking, event-driven architecture makes it an ideal choice for server-side development. Here are 7 project ideas to help beginners learn the fundamenta
6 min read