NestJS Basic
NestJS has many decorators, so it is easy to write syntactically correct code while misunderstanding when things happen. Two questions often get mixed up: when the application is assembled, and what a request passes through.
Separating these two questions is the fastest way to read Nest errors.
Two different moments
Section titled “Two different moments”| Moment | What happens | Read |
|---|---|---|
| Bootstrap | Nest reads module metadata, builds the graph, creates providers. Modules are only declarations and do not rerun per request | Modules, providers, controllers, DI container |
| A request arrives | The request passes through middleware, guards, interceptors, pipes to the handler, then back out through interceptors; errors branch off to filters | Request lifecycle |
The nastiest bugs live where the two meet: storing one request’s state in a field of a default provider (shared by every request), or putting request scope on a central provider so the whole tree is recreated per request (provider scopes).
Reading path
Section titled “Reading path”| Order | Article | Level | Answers |
|---|---|---|---|
| 1 | Modules, providers and controllers | Basic | The three roles, import/export, reading “can’t resolve dependencies” errors |
| 2 | DI container | Intermediate | Tokens, useClass vs useExisting, @Optional, async providers, dynamic modules |
| 3 | Provider scopes | Intermediate | How many instances, why request scope bubbles up, REQUEST or ALS |
| 4 | Request lifecycle | Intermediate | Layer order, the three paths, which errors filters catch |
| 5 | Custom decorators & Reflector | Intermediate | @Public(), @Roles(), override or merge metadata |
| 6 | Dynamic modules | Intermediate | forRoot/forRootAsync, why importing twice yields two instances |
| 7 | DTO validation | Basic | whitelist, nested DTOs, query params, mass assignment |
| 8 | Exception filters & error responses | Intermediate | One error format, mapping 23505 to 409, errors that never reach filters |
| 9 | Cross-service transactions (CLS) | Advanced | Keeping one connection across services, propagation, the forgotten-await trap |
Pick an article by situation
Section titled “Pick an article by situation”| Situation | Read |
|---|---|
| New to NestJS | Article 1, then article 4 |
UnknownDependenciesException at startup |
Modules, providers and controllers, the import/export section |
| Need two tokens pointing to the same instance | DI container, the useExisting section |
| Unsure whether logic belongs in a guard, interceptor or pipe | Request lifecycle |
Two requests overwrite each other’s currentTenant |
Provider scopes, then AsyncLocalStorage |
@Public() on a controller has no effect |
Custom decorators & Reflector |
A client sends role: "admin" and it gets through |
DTO validation |
| Every error has a different format, DB errors become 500 | Exception filters & error responses |
| A failure midway leaves half-written data across services | Cross-service transactions (CLS) |
| Suspect a handler is slowing other requests | No dedicated NestJS article yet; see Node.js event loop |
This section currently has the nine articles above. There are no articles yet on testing, microservices or GraphQL in NestJS.