Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

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.

16 questions: 4 warm-ups and 12 with model answers · Levels: Fresher to staff

Warm-up questions

Short questions with short answers, each answered in full.

  1. 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

  2. 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

  3. 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?

    Answered in the lesson Lifecycle hooks and shutdown signals

  4. 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.

  1. 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

    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.

    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
  2. 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

    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
  3. 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

    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
  4. 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

    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
  5. 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

    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
  6. 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

    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
  7. 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

    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
  8. 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

    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
  9. 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

    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
  10. 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

    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
  11. 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

    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
  12. 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

    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

All NestJS interview questions · The NestJS track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.