Bird
Raised Fist0
NestJSframework~10 mins

Dynamic modules in NestJS - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a dynamic module with a static method.

NestJS
import { Module, DynamicModule } from '@nestjs/common';

@Module({})
export class ConfigModule {
  static forRoot(): [1] {
    return {
      module: ConfigModule,
    };
  }
}
Drag options to blanks, or click blank then click option'
ADynamicModule
BModule
CProvider
DInjectable
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a plain Module instead of DynamicModule
Forgetting to return an object from the static method
2fill in blank
medium

Complete the code to inject a configuration value into the dynamic module.

NestJS
import { Module, DynamicModule } from '@nestjs/common';

@Module({})
export class ConfigModule {
  static forRoot(configValue: string): DynamicModule {
    return {
      module: ConfigModule,
      providers: [
        {
          provide: 'CONFIG_VALUE',
          useValue: [1],
        },
      ],
      exports: ['CONFIG_VALUE'],
    };
  }
}
Drag options to blanks, or click blank then click option'
A'configValue'
BConfigModule
CconfigValue
DCONFIG_VALUE
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal instead of the variable
Using the module name instead of the config value
3fill in blank
hard

Fix the error in the dynamic module import syntax.

NestJS
import { Module, DynamicModule } from '@nestjs/common';
import { ConfigModule } from './config.module';

@Module({
  imports: [ConfigModule.[1]({ apiKey: '123' })],
})
export class AppModule {}
Drag options to blanks, or click blank then click option'
Aregister
BforRoot
CuseFactory
Dcreate
Attempts:
3 left
💡 Hint
Common Mistakes
Using register or create which are not standard method names here
Using useFactory which is for providers, not module imports
4fill in blank
hard

Fill both blanks to create a dynamic module that imports another module and provides a service.

NestJS
import { Module, DynamicModule } from '@nestjs/common';
import { HttpModule } from '@nestjs/axios';

@Module({})
export class ApiModule {
  static forRoot(apiUrl: string): DynamicModule {
    return {
      module: ApiModule,
      imports: [[1]],
      providers: [
        {
          provide: 'API_URL',
          useValue: [2],
        },
      ],
      exports: ['API_URL'],
    };
  }
}
Drag options to blanks, or click blank then click option'
AHttpModule
BapiUrl
CConfigModule
D'apiUrl'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal instead of the variable for useValue
Importing the wrong module
5fill in blank
hard

Fill all three blanks to create a dynamic module that registers a provider with a factory and injects a token.

NestJS
import { Module, DynamicModule } from '@nestjs/common';

@Module({})
export class LoggerModule {
  static forRoot(level: string): DynamicModule {
    return {
      module: LoggerModule,
      providers: [
        {
          provide: 'LOG_LEVEL',
          useValue: [1],
        },
        {
          provide: 'LOGGER_SERVICE',
          useFactory: (logLevel: string) => new LoggerService(logLevel),
          inject: [[2]],
        },
      ],
      exports: [[3]],
    };
  }
}

class LoggerService {
  constructor(private level: string) {}
}
Drag options to blanks, or click blank then click option'
Alevel
B'LOG_LEVEL'
C'LOGGER_SERVICE'
Attempts:
3 left
💡 Hint
Common Mistakes
Injecting the service token instead of the log level token
Exporting the log level token instead of the service token

Practice

(1/5)
1. What is the main purpose of a dynamic module in NestJS?
easy
A. To automatically generate database schemas
B. To create modules without any providers or exports
C. To replace the need for controllers in an application
D. To allow configuration of modules with custom options at runtime

Solution

  1. Step 1: Understand dynamic module concept

    Dynamic modules let you pass custom options to configure the module when importing it.
  2. Step 2: Identify the main purpose

    This flexibility helps reuse modules with different settings instead of hardcoding them.
  3. Final Answer:

    To allow configuration of modules with custom options at runtime -> Option D
  4. Quick Check:

    Dynamic modules = configurable modules [OK]
Hint: Dynamic modules customize modules with options [OK]
Common Mistakes:
  • Thinking dynamic modules remove controllers
  • Believing dynamic modules auto-generate schemas
  • Assuming dynamic modules have no providers
2. Which of the following is the correct way to define a dynamic module method in NestJS?
easy
A. constructor(options: ModuleOptions): void { ... }
B. static forRoot(options: ModuleOptions): DynamicModule { ... }
C. export function createModule(options: ModuleOptions) { ... }
D. async initialize(options: ModuleOptions): Promise<void> { ... }

Solution

  1. Step 1: Recall dynamic module pattern

    Dynamic modules use a static method, commonly named forRoot, returning a DynamicModule object.
  2. Step 2: Match method signature

    Only a static method can be called without creating an instance, fitting the dynamic module pattern.
  3. Final Answer:

    static forRoot(options: ModuleOptions): DynamicModule { ... } -> Option B
  4. Quick Check:

    Dynamic module method = static forRoot [OK]
Hint: Dynamic modules use static forRoot method [OK]
Common Mistakes:
  • Using constructor instead of static method
  • Defining dynamic module as a normal function
  • Making dynamic module method async without returning DynamicModule
3. Given this dynamic module code, what will be the value of the exported provider's token?
export class LoggerModule {
  static forRoot(prefix: string): DynamicModule {
    return {
      module: LoggerModule,
      providers: [
        { provide: 'LOGGER_PREFIX', useValue: prefix },
      ],
      exports: ['LOGGER_PREFIX'],
    };
  }
}
medium
A. 'prefix'
B. 'LoggerModule'
C. 'LOGGER_PREFIX'
D. 'useValue'

Solution

  1. Step 1: Identify provider token

    The provider uses the key 'provide' with value 'LOGGER_PREFIX' which is the token for injection.
  2. Step 2: Confirm exported token

    The exports array includes 'LOGGER_PREFIX', so this token is available outside the module.
  3. Final Answer:

    'LOGGER_PREFIX' -> Option C
  4. Quick Check:

    Provider token = 'LOGGER_PREFIX' [OK]
Hint: Provider token is the 'provide' string value [OK]
Common Mistakes:
  • Confusing module name with provider token
  • Using variable name 'prefix' as token
  • Thinking useValue is the token
4. Identify the error in this dynamic module definition:
export class CacheModule {
  static forRoot(options: { ttl: number }): DynamicModule {
    return {
      module: CacheModule,
      providers: [
        { provide: 'CACHE_TTL', useFactory: (options) => options.ttl },
      ],
      exports: ['CACHE_TTL'],
    };
  }
}
medium
A. The useFactory function cannot access options parameter directly
B. The module property should be a string, not a class
C. Exports array must include providers, not strings
D. Static methods cannot return DynamicModule

Solution

  1. Step 1: Analyze useFactory context

    The useFactory function runs later and cannot directly access the outer options variable without injection.
  2. Step 2: Identify correct pattern

    To use options inside useFactory, it must be passed via inject or closure properly; direct access causes runtime error.
  3. Final Answer:

    The useFactory function cannot access options parameter directly -> Option A
  4. Quick Check:

    useFactory needs inject or closure for options [OK]
Hint: useFactory can't directly use outer variables [OK]
Common Mistakes:
  • Thinking module property must be string
  • Believing exports must list provider objects
  • Assuming static methods can't return DynamicModule
5. You want to create a dynamic module that accepts a configuration object and provides two services using that config. Which approach correctly implements this pattern?
hard
A. Use a static forRoot method returning a DynamicModule with providers using useValue for config and export both services
B. Create a constructor in the module class that sets config and exports services directly
C. Define services inside the module without any configuration and import them normally
D. Use a global module without any providers and configure services externally

Solution

  1. Step 1: Understand dynamic module pattern for config

    Dynamic modules use a static method (like forRoot) to accept config and return a DynamicModule object with providers.
  2. Step 2: Provide config and services properly

    Use useValue or useFactory to inject config into providers, and export services to make them available outside.
  3. Final Answer:

    Use a static forRoot method returning a DynamicModule with providers using useValue for config and export both services -> Option A
  4. Quick Check:

    Dynamic module with static forRoot and providers = Use a static forRoot method returning a DynamicModule with providers using useValue for config and export both services [OK]
Hint: Use static forRoot with providers and exports [OK]
Common Mistakes:
  • Trying to configure modules via constructor
  • Not exporting services after providing them
  • Using global module without providers for config
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard