NestJS Module 4
Modules, dynamic modules and lifecycle interview questions
Interview questions on modules, dynamic modules and lifecycle: warm-ups, then what each harder question tests and how a good answer starts.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher to mid
What does it mean that a Nest module encapsulates its providers, and what changes when a module exports one?
Answered in the lesson Modules, encapsulation and shared modules
-
Fresher to mid
What is the difference between forRoot() and register() in a Nest dynamic module, and does Nest treat them differently?
Answered in the lesson Dynamic modules and ConfigurableModuleBuilder
-
Fresher to mid
In what order does Nest call onModuleInit() across the modules of an app, and how does the shutdown order relate to it?
-
Fresher to mid
What is the difference between NestFactory.create() and NestFactory.createApplicationContext(), and when would you use the second?
Answered in the lesson Standalone apps, CLI commands and the REPL
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Fresher to mid
You moved KitchensService and CapacityCache out of AppModule into a new KitchensModule, and now the app stops at start-up with "Nest can't resolve dependencies of the OrdersService (?)". How do you find the cause, fix it, and keep the next split from breaking the same way?
- What it tests
- Reading the message part by part, the visibility rules of imports and exports, and a check that catches broken wiring before a deploy.
- First step
- Read the message part by part: OrdersService is the class being built, KitchensService at index [0] is the token, and the module named last is where the search started. The provider now lives in the new module, so check that KitchensModule exports it, then that the module named in the message imports KitchensModule.
- Follow-ups
-
- Why not add KitchensService to the providers of OrdersModule?
- What would NEST_DEBUG show in each case?
- Which test catches this before a deployment?
From the lesson Modules, encapsulation and shared modules
Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.Opening the model answers with your pass…
Model answers open with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
TiffinTrail allows 5 orders a minute per customer, counted in memory by a RateLimiter. Orders can be placed through OrdersModule and through the "order again" button of MenusModule. After a refactor, a customer placed 10 in one minute. Both modules list RateLimiter in providers. Explain the cause, the fix and a test that guards it.
- What it tests
- Knowing that each module that lists a class builds its own instance, moving shared state behind one exported provider, and testing instance identity across modules.
- First step
- Count the instances: a class listed in the providers of two modules is built twice, so each path counts its own 5. Make one module own RateLimiter and export it, and have both features import that module.
- Follow-ups
-
- How do you prove that there were two instances?
- Does the fix still hold with three copies of the app behind a load balancer?
- Would @Global() be a better fix?
From the lesson Modules, encapsulation and shared modules
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A teammate proposes marking DatabaseModule, ConfigModule, MailModule and KitchensModule with @Global() so that feature modules stop importing them. Which would you accept, which would you push back on, and why?
- What it tests
- Weighing the convenience of global modules against hidden dependencies, testability and ownership, and knowing the alternatives: explicit imports and a core module that re-exports.
- First step
- Sort the four by what they are and who uses them: configuration is read by nearly every module, a database by many, a mail sender by a few, and KitchensService belongs to one feature. Then ask what a test that builds one feature module alone would have to import for each choice.
- Follow-ups
-
- What does a global module cost in tests?
- Where would you register the ones you accept?
- What would you offer instead for DatabaseModule?
From the lesson Modules, encapsulation and shared modules
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
Three internal apps will share an SmsModule. Each passes a sender id and an API key; one app reads them from a secrets store at start-up, and a wrong sender id must stop the deploy rather than fail the first SMS. Describe the module's API, how you would build it, and where each check runs.
- What it tests
- Designing a dynamic module's public API, choosing the sync and async forms, using ConfigurableModuleBuilder, and failing fast on bad options.
- First step
- Start from the callers: each app configures the module once, so it offers forRoot() and forRootAsync(), and the secrets-store app needs the async form with a factory. Then ask where one check could see the options of both forms before the first SMS is sent.
- Follow-ups
-
- Why not check the options inside forRoot()?
- How would a test replace the gateway?
- Should the module be global?
From the lesson Dynamic modules and ConfigurableModuleBuilder
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
After a refactor, each instance of an app opens 20 database connections instead of 10. You find DatabaseModule.forRoot() with the same settings in OrdersModule and in BillingModule. Explain why there are two pools, and how you would fix it so that it stays fixed.
- What it tests
- Knowing how Nest tells two dynamic modules apart, configuring a module once and sharing it, and making the instance count testable.
- First step
- Count the modules, not the settings: ask how many modules the two forRoot() calls create, and how many pool providers come with them, before you compare what the two calls pass.
- Follow-ups
-
- Why do equal settings not merge into one module?
- Would changing how Nest identifies modules be a good fix?
- Which test would catch it?
From the lesson Dynamic modules and ConfigurableModuleBuilder
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
SmsModule, built with ConfigurableModuleBuilder, is configured with forRootAsync({ inject: [ConfigService], useFactory }). Start-up stops: Nest can't resolve dependencies of the SMS_MODULE_OPTIONS (?), ConfigService at index [0]. ConfigModule.forRoot() is in AppModule's imports. Why, and what are two ways to fix it?
- What it tests
- Knowing where the options factory is registered, how the imports of forRootAsync() make providers visible to it, and what making ConfigModule global trades away.
- First step
- Ask where the provider named in the message is registered, and so in which module Nest searched for ConfigService. Then recall what a class of a module can see: its own module's providers, the exports of that module's imports, and global modules.
- Follow-ups
-
- Why does ConfigModule.forRoot() in AppModule not help?
- Which fix would you choose, and why?
- What would useExisting change?
From the lesson Dynamic modules and ConfigurableModuleBuilder
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 2 minutes
-
Mid to senior
During every deploy, TiffinTrail's kitchen worker loses the job it is processing: the job is marked started and never finished. The worker is a Nest application context that takes jobs from a queue. What is happening, and how would you change it so that a deploy loses nothing?
- What it tests
- Knowing what SIGTERM does to a Nest process without shutdown hooks, using the three shutdown hooks to drain, the module order of the hooks, and safe retries.
- First step
- Start from the signal: a deploy sends the old process SIGTERM, and without enableShutdownHooks() Node.js ends it at once, with exit status 143, in the middle of the job. Then decide what each of the three shutdown hooks should do for a worker that holds a job, a queue connection and a database pool.
- Follow-ups
-
- Which hook does what, and why in that order?
- What if a job can take longer than the platform's grace period?
- How do you test it?
From the lesson Lifecycle hooks and shutdown signals
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
Deploys of a Nest service on Kubernetes take about 30 seconds longer than they should, and the old Pods end with SIGKILL. The logs show "closing the broker connection" and nothing after it. What do you suspect, how do you confirm it, and what do you change?
- What it tests
- Reading a shutdown that never completes, knowing how Nest waits for its shutdown hooks, and budgeting shutdown time against the grace period.
- First step
- Thirty seconds is Kubernetes's default grace period, so the process never ended by itself: something in the shutdown sequence was still waiting. Ask which hook was running when the logs stopped, and what it was waiting for.
- Follow-ups
-
- How would you reproduce it on a laptop?
- What deadline would you choose?
- What is lost when SIGKILL ends the process?
From the lesson Lifecycle hooks and shutdown signals
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Senior to staff
During rolling deploys, clients of a Nest API behind a load balancer see a few refused connections each time a Pod stops, although shutdown hooks are enabled and in-flight requests finish. Explain where the errors come from in Nest's shutdown sequence, and what you would change.
- What it tests
- Knowing when Nest's HTTP server stops accepting connections during shutdown, how a load balancer learns that a Pod is going away, and the server options that change what late requests see.
- First step
- Place the server's close in the sequence of the shutdown hooks, then compare that moment with the moment the load balancer stops sending this Pod requests. The errors live in the gap between the two.
- Follow-ups
-
- Why would a wait in one of the hooks help, and in which one?
- When would you answer late requests with 503?
- What do keep-alive connections change?
From the lesson Lifecycle hooks and shutdown signals
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A nightly billing command built with nest-commander reports success to the scheduler every night, yet some mornings no invoices exist, and the logs of those nights show an error message. What is the likely cause, and how do you make failures visible to the scheduler and to people?
- What it tests
- Knowing how nest-commander handles an error thrown by a command, how exit statuses reach schedulers, and how to test the failure path.
- First step
- Separate "the command failed" from "the process reported failure": the error was printed, so the command threw; the scheduler saw success, so the exit status was 0. Then look at what nest-commander does with an error that a command throws, and what sets the exit status.
- Follow-ups
-
- What does nest-commander's default error handling do?
- Which other ways out of a command skip your handler?
- How would you test the failure path?
From the lesson Standalone apps, CLI commands and the REPL
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 2 minutes
-
Mid to senior
A data-fix script built on createApplicationContext() ends with exit status 1 in CI and prints nothing at all; on a laptop it works. How do you find out what failed, and what do you change so that it can never fail silently again?
- What it tests
- Knowing what Nest does with a start-up error by default, how a script's logger options change what it prints, and how a script should report its own failures.
- First step
- Status 1 with no output means the process chose to end, and something kept the reason from being printed. Look at how the script creates its context: which options it passes, and where a start-up error would be written.
- Follow-ups
-
- Why does it work on a laptop?
- What would you put in place of logger false?
- When would you turn abortOnError off?
From the lesson Standalone apps, CLI commands and the REPL
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to staff
During a 20-minute outage, one kitchen's display missed 40 orders. The on-call engineer must send them again now, and the same may be needed after the next outage. Would you use the Nest REPL, a one-off script or a CLI command? Argue for one, and say what you would build in to make it safe.
- What it tests
- Choosing between the REPL, scripts and commands for an operations job that changes data, and designing it to be safe, repeatable and checkable.
- First step
- Start from the risks, not the tools: the job writes, may run twice, will run again after the next outage, and runs under pressure. Then ask which of the three can be reviewed in advance, rehearsed with a dry run, and leaves a record of what it did.
- Follow-ups
-
- What would the dry run print?
- How do you make a second run harmless?
- Where does the REPL still help?
From the lesson Standalone apps, CLI commands and the REPL
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes