nest: ConfigurableModuleBuilder "alwaysTransient: false" (@default) doesn't work

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

The console log is printed only once, unless alwaysTransient is set to true.

// my-custom-module.ts

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

import { ConfigurableModuleBuilder } from '@nestjs/common';

export const {
  ConfigurableModuleClass,
  MODULE_OPTIONS_TOKEN: MY_CUSTOM_MODULE_OPTIONS,
} = new ConfigurableModuleBuilder<{
  baseUrl?: string;
}>().build();

@Module({})
export class MyCustomModule extends ConfigurableModuleClass {
  constructor(
    @Inject(MY_CUSTOM_MODULE_OPTIONS)
    private readonly options,
  ) {
    console.log(options);
    super();
  }
}

// app.module.ts
@Module({
  imports: [
    MyCustomModule.register({ baseUrl: 'https://google.com' }),
    MyCustomModule.register({ baseUrl: 'https://google1.com' }),
    MyCustomModule.register({ baseUrl: 'https://google2.com' }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Steps to reproduce

No response

Expected behavior

Expecting the console log to run 3 times because the options are different in the providers array (as stated in the official nestjs advanced concepts course).

Package

Other package

No response

NestJS version

10.3.0

Packages versions

[System Information]
OS Version     : Linux 6.1.69-1-MANJARO
NodeJS Version : v20.8.0
NPM Version    : 10.1.0 

[Nest CLI]
Nest CLI Version : 10.3.0 

[Nest Platform Information]
platform-express version : 10.3.0
mapped-types version     : 2.0.4
schematics version       : 10.1.0
testing version          : 10.3.0
common version           : 10.3.0
core version             : 10.3.0
cli version              : 10.3.0

[Warnings]
The following packages are not in the same minor version
This could lead to runtime errors
* Under version 10.3
- @nestjs/platform-express 10.3.0
- @nestjs/common 10.3.0
- @nestjs/core 10.3.0
* Under version 10.1
- @nestjs/schematics 10.1.0

Node.js version

v20.8.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

About this issue

  • Original URL
  • State: closed
  • Created 5 months ago
  • Comments: 20 (9 by maintainers)

Most upvoted comments

@micalevisk Yeah, this is a bug introduced by my PR, alwaysTransient works because of: https://github.com/nestjs/nest/pull/12753/files#diff-22f1ab07f43abbd45f270de55b6cd35da90c02fe92cee99f67d51a37b85002c8R209-R220 which makes the module unique.

That piece of code worked before because we used those objects to differentiate each module, if he use the async version, then it will have the same issue.

We can basically revert the change for that module only, the easiest/quickest fix, the other changes we don’t need to revert.

We can also go back to the conversation started at https://github.com/nestjs/nest/pull/12898, this issue could be fixed using that feature.

Actually, seems to be a semi-recent bug in the latest version of @nestjs/common@10.3.0.

image

EDIT: yeah

Notice that it works as expected if I downgrade to version 9 for example.

image

Here you go, hope that’s enough 😃

Yeah, already searched in the releases, wasn’t mentioned. (Another reason I suspect this to be a bug).

Okay, I’ll make a repo in a minute. I can also test with different nest versions and will report up until what version it works as expected.

That’s the correct video you’re referencing, but it’s the wrong timestamp. I can provide the screenshots in a sec.