Best practices for NestJS Module Structure
.cursorrules in your project root# NestJS Module Structure
=====================================
## Core Principles
-------------------
* A NestJS module should be a self-contained unit of functionality.
* Each module should have a single responsibility and a well-defined interface.
* Modules should be loosely coupled and highly cohesive.
## Code Style Guidelines
-----------------------
### Naming Conventions
* Module names should be in PascalCase (e.g., `UsersModule`).
* Module file names should be in kebab-case (e.g., `users.module.ts`).
* Module class names should be in PascalCase (e.g., `UsersModule`).
* Module variables and functions should be in camelCase (e.g., `userService`).
### Typing
* Use the `@Module` decorator to define a NestJS module.
* Use the `imports` array to import other modules.
* Use the `controllers` array to define controllers.
* Use the `providers` array to define providers (services, repositories, etc.).
* Use the `exports` array to export providers.
## Best Practices
------------------
### Module Organization
* Keep each module in its own directory.
* Use a consistent directory structure across modules (e.g., `modules/users`, `modules/products`).
* Keep module files (e.g., `users.module.ts`, `users.controller.ts`) in the same directory.
### Module Dependencies
* Use the `imports` array to import other modules.
* Avoid circular dependencies between modules.
* Use lazy loading to load modules only when needed.
### Module Configuration
* Use the `forRoot` and `forFeature` methods to configure modules.
* Use environment variables to configure module settings.
## Common Pitfalls to Avoid
---------------------------
### Overly Complex Modules
* Avoid large, complex modules that are difficult to maintain.
* Break down complex modules into smaller, more focused modules.
### Tight Coupling
* Avoid tightly coupling modules together.
* Use interfaces and dependency injection to decouple modules.
### Duplicate Code
* Avoid duplicating code across modules.
* Extract common functionality into shared modules or utilities.
### Incorrect Module Loading
* Avoid loading modules unnecessarily.
* Use lazy loading to load modules only when needed.
Example of a well-structured NestJS module:
```typescript
// users.module.ts
import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserEntity } from './user.entity';
@Module({
imports: [TypeOrmModule.forFeature([UserEntity])],
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService],
})
export class UsersModule {}
```Comprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.
Cursor
coding
AI coding rules customize how Cursor generates and refactors code for your project. Follow these steps to install NestJS Module Structure.
.cursor/rules, for Windsurf use .windsurfrulesComprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.