All interview questions

NestJS Interview Questions

Follow a request through a NestJS application and explain which responsibility belongs where. Then trace the same boundaries when a provider or deployment fails.

Try answering aloud before opening each explanation.

Try the NestJS practice demo

Foundations

What makes a provider in one NestJS module available to another module?

Export the provider from its declaring module and import that module in the consumer. Registering the same class separately in the consumer can create a different instance instead of reusing the exported provider.

Why are TypeScript interfaces not usable as injection tokens at runtime?

Interfaces are erased. Use a class, symbol or string as a runtime token, register a provider with that token and inject it explicitly where necessary.

Where should domain rules live: in a controller or a provider?

Let controllers translate transport requests into calls and responses. Put reusable business rules in a service or domain component so other entry points can use them without HTTP coupling.

How does a guard differ from a pipe?

A guard decides whether execution may continue based on context, typically for access control. A pipe validates or transforms arguments before the handler receives them.

When would you use an exception filter instead of an interceptor?

A filter translates selected uncaught exceptions into transport responses. An interceptor surrounds handler execution and can time calls or transform successful results as well as handle errors.

Applied

Why might a guard see raw parameters before a validation pipe changes them?

Guards run before pipes. If an access decision depends on an input, validate it independently in the guard or move the resource-specific decision to a service with validated arguments.

Why can an interface typed as a DTO fail to validate incoming JSON?

The interface has no runtime metadata. Use a runtime schema or a DTO class with validation decorators and a configured validation pipe; typing a parameter alone is not validation.

When does request scope affect more than one provider?

A consumer that depends on a request-scoped provider becomes request-scoped through the dependency chain. Prefer explicit context parameters for simple use cases when per-request construction adds cost.

What is the benefit of a dynamic module?

It lets a module produce providers and exports from configuration supplied at import time. For example, a module can construct a shared client with different options without duplicating registration logic.

What problem does useExisting solve compared with useClass?

UseExisting aliases a token to an already registered provider. UseClass tells the container to instantiate the class for the configured provider, which may create a second instance.

Production scenarios

How do you diagnose a provider dependency that Nest cannot resolve?

Check the reported parameter index and token, then follow the declaring module's imports and exports. Verify the token is identical at runtime, especially for symbols and modules reached through multiple package copies.

What should you try before resolving a service cycle with forwardRef?

Look for a shared responsibility that belongs in a third provider or an orchestration layer. Forward references can unblock unavoidable cycles but make initialization and testing harder to reason about.

How do you test an HTTP endpoint while replacing a downstream service?

Build a testing module and override the same token used in production. Initialize the application with its real global pipes and filters so the HTTP test exercises routing and validation rather than only calling controller methods.

Why is a request-specific property on a default-scoped provider unsafe?

Default providers are normally shared across requests. Concurrent requests can overwrite the property; pass the value as an argument or use a properly scoped context.

How do you ensure a server shutdown closes consumers and database connections?

Register the appropriate lifecycle hooks and enable signal-driven shutdown hooks where required. Stop new work, drain what is in flight and close shared resources in a tested order.

Further reading

Official documentation

Related question sets