我们正在为不同的服务运行几个NestJS应用程序。它们都共享一些公共代码,在我们的例子中是ConfigModule和CacheModule。我想打破这些并将它们放在公司的“通用”npm包中,以便最大限度地减少代码复制。
我遇到了一个错误:Nest cannot export a component/module that is not a part of the currently processed module (ConfigModule). Please verify whether each exported unit is available in this particular contex
。
关于问题到底是什么,我有点迷茫。任何帮助深表感谢。
在app.ts
服务A:
import { ConfigModule } from '@company/npm-common';
...
@Module({
imports: [ConfigModule, ...],
})
export class AppModule {}
现在@company/npm-common
是用file:../npm-common
在package.json中导入的
npm-common/index.ts
:
export * from './Config';
npm-common/Config/index.ts
export { ConfigService } from './config.service';
export { ConfigModule } from './config.module';
npm-common/Config/config.service
:
import { Global, Module } from '@nestjs/common';
import { ConfigService } from './config.service';
@Global()
@Module({
providers: [
{
provide: ConfigService,
useValue: new ConfigService(),
},
],
exports: [ConfigService],
})
export class ConfigModule {}
这个问题的愚蠢答案是主应用程序和外部模块加载了不同版本的NestJS。似乎5.5版本在这个意义上与版本6不兼容。