Expert guidance for NestJS v11 projects with TypeScript, Express, and pnpm. Covers architecture patterns, CRUD operations, testing, and debugging.
Provides expert assistance for NestJS v11 projects using TypeScript, Express, and pnpm package manager. Based on production patterns from nestjs_101 starter project.
When working with NestJS projects, follow this structure:
1. **Entry Point** (`src/main.ts`):
- Bootstrap the application using `NestFactory.create()`
- Default port 3000, override with `PORT` environment variable
- Enable CORS and other global middleware here
2. **Module Organization** (`*.module.ts`):
- Root module imports all feature modules
- Use `@Module()` decorator with `imports`, `controllers`, `providers`
- Follow dependency injection pattern
3. **Controllers** (`*.controller.ts`):
- Use `@Controller()` with route prefixes
- Decorate methods with `@Get()`, `@Post()`, `@Put()`, `@Delete()`, etc.
- Extract route parameters using `@Param('id')`
- Extract request body using `@Body()`
- Inject services via constructor
4. **Services** (`*.service.ts`):
- Mark with `@Injectable()` decorator
- Contain business logic (controllers should be thin)
- Injected into controllers/other services via constructor
Always use these suffixes:
Ensure `tsconfig.json` includes:
```json
{
"compilerOptions": {
"target": "ES2023",
"module": "NodeNext",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"strictNullChecks": true
}
}
```
Provide these commands when asked about development workflow:
```bash
pnpm start:dev
pnpm routes
pnpm test
pnpm test:e2e
pnpm lint
```
**Unit Tests** (`*.spec.ts`):
**E2E Tests** (`test/*.e2e-spec.ts`):
When creating new features:
```typescript
// user.service.ts
@Injectable()
export class UserService {
findAll() { /* ... */ }
findOne(id: string) { /* ... */ }
}
// user.controller.ts
@Controller('users')
export class UserController {
constructor(private readonly userService: UserService) {}
@Get()
findAll() {
return this.userService.findAll();
}
@Get(':id')
findOne(@Param('id') id: string) {
return this.userService.findOne(id);
}
}
// user.module.ts
@Module({
controllers: [UserController],
providers: [UserService],
})
export class UserModule {}
```
If routes aren't registering correctly:
1. Install `nestjs-spelunker` if not present: `pnpm add nestjs-spelunker`
2. Run route introspection: `pnpm routes`
3. Check debug output in `scripts/routes.ts` or `scripts/debug.ts`
4. Verify controller is registered in module's `controllers` array
5. Verify module is imported in app module's `imports` array
1. **Keep controllers thin** - Business logic belongs in services
2. **Use dependency injection** - Always inject via constructor, not manual instantiation
3. **Co-locate tests** - Unit tests next to source files for easy maintenance
4. **Use decorators** - Leverage NestJS decorators instead of manual Express routing
5. **Follow naming conventions** - Use prescribed suffixes for all files
6. **Type everything** - Leverage TypeScript's type system fully
When users need deeper information, direct them to:
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/nestjs-11-starter-guide/raw