This document outlines a robust, scalable folder structure for enterprise-level NestJS applications. It supports best practices, domain-driven design (DDD), third-party integrations, environment management, and advanced tooling.
# Install PostgreSQL (Server + Client Tools)
$ sudo apt update
$ sudo apt install postgresql postgresql-contrib
# Start PostgreSQL Service
$ sudo systemctl enable postgresql
$ sudo systemctl start postgresql
# Set the postgres Password
# Switch to postgres system user (Linux/macOS)
$ sudo -u postgres psql
# Inside psql:
$ ALTER USER postgres PASSWORD 'your_new_password';
npm i cookie-parser express-session bcrypt --loglevel=silly
npm i --save @nestjs/swagger @nestjs/axios axios @nestjs-modules/mailer nodemailer @nestjs/jwt @nestjs/passport passport passport-local @nestjs/websockets @nestjs/platform-socket.io class-validator class-transformer @nestjs/typeorm typeorm pg @nestjs/config
npm i --save-dev @types/passport-local @types/nodemailer
npm i -D @types/cookie-parser @types/multer @types/express-session @types/bcrypt
src/
βββ main.ts
βββ app.module.ts
βββ app.service.ts
βββ modules/ # Domain-driven feature modules
β βββ auth/
β β βββ auth.module.ts
β β βββ auth.controller.ts
β β βββ auth.service.ts
β β βββ auth.strategy.ts
β β βββ auth.guard.ts
β β βββ dto/
β β βββ interfaces/
β β βββ enums/
β β
β βββ users/
β β βββ users.module.ts
β β βββ users.service.ts
β β βββ users.controller.ts
β β βββ entities/
β β βββ dto/
β β βββ interfaces/
β β βββ enums/
β β
β βββ ... (other features like orders, products, etc.)
βββ common/ # Shared logic
β βββ decorators/
β βββ filters/
β β βββ http-exception.filter.ts
β βββ interceptors/
β βββ guards/
β βββ middlewares/
β βββ pipes/
β βββ utils/
βββ infrast
7ED6
ructure/ # External system integrations
β βββ logger/
β β βββ logger.service.ts
β βββ mailer/
β βββ storage/
β βββ database/
β β βββ database.module.ts
β β βββ typeorm.config.ts
β β βββ seeds/
β β βββ migrations/
β βββ ...
βββ config/ # Environment configuration
β βββ config.module.ts
β βββ config.service.ts
β βββ validation.ts # Joi schema for env validation
βββ shared/ # Shared constants, types, enums
β βββ constants/
β βββ interfaces/
β βββ enums/
βββ third-party/ # External services and APIs
β βββ cloudinary/
β β βββ cloudinary.module.ts
β β βββ cloudinary.service.ts
β β βββ cloudinary.constants.ts
β βββ ...
βββ typings/ # Type overrides (e.g. Express user)
β βββ express.d.ts.env # Main environment file
.env.sample # Sample for team/CI use
.prettierrc # Code formatting rules
.eslintrc.js # Linting rules
Dockerfile # Docker container setup
.dockerignore
nestjs-cli.json
package.json
README.md- Group by domain (not by type)
- Use
modules/for all business features - Use
common/for cross-cutting concerns (filters, guards, pipes, etc.) - Use
infrastructure/for low-level implementations (DB, logging, storage) - Use
third-party/for external API wrappers (Stripe, Cloudinary, etc.) - Use
config/to centralize environment config with validation - Use
typings/for TypeScript augmentations
Let me know if you need a code scaffold or starter repo based on this!
nestjs-pro/
βββ src/
β βββ app.module.ts # Root module
β βββ main.ts # Entry point
β βββ config/ # Centralized configuration files
β β βββ app.config.ts
β β βββ database.config.ts
β β βββ ...
β βββ common/ # Common/shared utils and constants
β β βββ constants/
β β βββ decorators/
β β βββ filters/
β β βββ guards/
β β βββ interceptors/
β β βββ utils/
β βββ modules/ # Feature modules
β β βββ auth/
β β β βββ dto/
β β β βββ entities/
β β β βββ auth.controller.ts
β β β βββ auth.service.ts
β β β βββ auth.module.ts
β β βββ users/
β β βββ addresses/
β β βββ ...
β βββ jobs/ # Background tasks
β β βββ queues/ # BullMQ or Redis jobs
β β β βββ email.queue.ts
β β β βββ report.queue.ts
β β βββ cron/ # Scheduled jobs
β β β βββ cleanup.cron.ts
β β β βββ analytics.cron.ts
β β βββ workers/ # Long-running or async workers
β β β βββ pdf.worker.ts
β β β βββ ...
β β βββ jobs.module.ts
β βββ events/ # Event-driven architecture
β β βββ handlers/
β β βββ emitters/
β β βββ events.module.ts
β βββ notifications/ # Notification channels
β β βββ mail/
β β βββ sms/
β β βββ push/
β β βββ notifications.module.ts
β βββ graphql/ # Optional: GraphQL setup
β β βββ resolvers/
β β βββ schemas/
β β βββ graphql.module.ts
β βββ logging/ # Winston or Pino logger config
β β βββ logger.service.ts
β βββ metrics/ # Prometheus / monitoring
β β βββ metrics.module.ts
β βββ health/ # Health checks & readiness
β β βββ health.controller.ts
β βββ webhooks/ # Webhook listeners
β β βββ payment.webhook.ts
β β βββ ...
β βββ schedulers/ # Scheduler-based jobs
β β βββ sync.scheduler.ts
β βββ main.ts
βββ test/ # Unit and e2e tests
β βββ auth.e2e-spec.ts
β βββ users.service.spec.ts
β βββ ...
βββ .env # Environment variables
βββ nest-cli.json
βββ tsconfig.json
βββ package.json
βββ README.md