Current behavior
I cannot have two modules with same name in application. Only the first module will be available in the application. In my case, this is the AModule
from "./my-a-module".
Input Code
Third party library@Module({
providers: [AService]
exports: [AService]
})
export class AModule {
}
@Module({
imports: [AModule],
providers: [BService]
exports: [BService]
})
export class BModule {
}
My code
import { BModule } from "third-party-library";
import { AModule } from "./my-a-module";
@Module({
imports: [
AModule,
BModule
]
})
export class CModule {
}
Expected behavior
I am not sure if this is a bug. But I was expecting that I could have two separate Nest modules with same name.
Environment
"@nestjs/mon": "^6.9.0",
"@nestjs/core": "^6.9.0"
For Tooling issues:
- Node version: v10.16.0
- Platform: Linux
Current behavior
I cannot have two modules with same name in application. Only the first module will be available in the application. In my case, this is the AModule
from "./my-a-module".
Input Code
Third party library@Module({
providers: [AService]
exports: [AService]
})
export class AModule {
}
@Module({
imports: [AModule],
providers: [BService]
exports: [BService]
})
export class BModule {
}
My code
import { BModule } from "third-party-library";
import { AModule } from "./my-a-module";
@Module({
imports: [
AModule,
BModule
]
})
export class CModule {
}
Expected behavior
I am not sure if this is a bug. But I was expecting that I could have two separate Nest modules with same name.
Environment
"@nestjs/mon": "^6.9.0",
"@nestjs/core": "^6.9.0"
For Tooling issues:
- Node version: v10.16.0
- Platform: Linux
Share
Improve this question
asked Nov 8, 2019 at 9:33
MikeMike
6631 gold badge11 silver badges23 bronze badges
5
-
you cannot have the same variable name refer to two different values at the same time either. What made you think you could have two modules with the same name in the same application? It's different from locally reusing variable names. Quote the docs:
In Nest, modules are singletons by default, and thus you can share the same instance of any provider between multiple modules effortlessly. Every module is automatically a shared module.
That implies to me that they cannot have the same name, since else how will you know which module is which? – Shilly Commented Nov 8, 2019 at 9:46 - @Shilly "you cannot have the same variable name refer to two different values at the same time either" But what if I have two variables in different Node.js modules? Or even in different NPM packages? I want to have different Nest.js modules with same name, but in different NPM packages. And what are benefits of this limitation? Maybe I can override any module in an application? This is not obvious. And I can have two or more DYNAMIC modules with same name in application. How should I override dynamic modules? – Mike Commented Nov 8, 2019 at 10:10
- How would you know which module is which if two modules can have the exact same identifier? Compare this to global variables and local variables. Inside a module, all your variables are local, so you can reuse the same names as variables inside other modules ( local scope ). But the module itself will also be used inside a scope, namely inside the app. So from the app point of view, the modules live in the same scope. Cant you just use versioning and have different NPM modules use different versions in the package.json ? Dynamic modules: docs.nestjs./fundamentals/dynamic-modules – Shilly Commented Nov 8, 2019 at 10:22
- @Shilly see the fix: github./nestjs/nest/pull/3363 – Mike Commented Nov 8, 2019 at 10:31
- Guess I'm misunderstanding how nest.js works if this is a bug and should be possible. ;) I have the opposite point of view where I do not understand why I should be able to have two different modules have the exact same name in the same app.Or how I would differentiate between the two if I cannot look at their name / id / identifier. – Shilly Commented Nov 8, 2019 at 10:39
2 Answers
Reset to default 3See related issue https://github./nestjs/nest/issues/3362 and the fix https://github./nestjs/nest/pull/3363
Ok so when nest builds it's container each class is instanced and the class prototype is used as a 'Symbol' to uniquely identify the module. Modules in nestjs are not the same as export modules in ES6 MJS. So What you're doing here is
@Module({
providers: [TestProvider],
})
export class ModuleA {}
@Module({})
export class ModuleA {}
const dummyContainer = {
[ModuleA.constructor.name] = new ModuleA(),
[ModuleA.constructor.name] = new ModuleA(),
};
However, in another answer I made, you could again use DynamicModule
to change the key value in the example above like so
@Module({})
export class ModuleA {
public static forRoot(): DynamicModule {
return {
module: 'UNIQUE_IDENTIFIER',
providers: [TestProvider],
};
}
}
@Module({})
export class ModuleA{}
Now when using the above example like so
import {ModuleA as ModuleB} from './for-root-example';
import {ModuleA} from './moduleA';
@Module({
imports: [
ModuleA,
ModuleB.forRoot(),
],
})
ModuleA as MobuleB should have the unique identifier key for the module when importing and therefore can have the same class name. To be honest though, I really wouldn't remend this.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744936334a4602047.html
评论列表(0条)