NestJS - Nest 无法解析依赖项 - 使用 npm/yarn 链接时
我在尝试通过 npm link 命令在我的应用程序中使用多个包时遇到了死胡同。 我有一个核心项目(它生成发布到 npm 的工件)和一个使用核心库的应用程序项目。使用 npm 中的库时一切都很好。
在我本地使用 npm 链接/yarn 链接不起作用。我已经使用不同的配置玩了两天(将所有内容都放在 import/providers/export 上),但我找不到一种方法让应用程序从链接方法开始。
核心模块
@Module({
imports: [
TypeOrmModule.forFeature([CountryRepository])
],
providers: [
CountryService
],
exports: [
TypeOrmModule.forFeature([CountryRepository]),
CountryService
],
})
应用程序模块
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: '',
password: '',
database: '',
entities: [Country]
}),
CoreModule
],
controllers: [
CountryController,
],
providers: [
CountryService,
]
})
export class AppModule {
}
这是当应用程序以创建的链接启动时出现的错误
[Nest] 12072 - 02/25/2022, 11:30:10 PM ERROR [ExceptionHandler] Nest can't resolve dependencies of the CountryRepository (?). Please make sure that the argument Connection at index [0] is available in the TypeOrmModule context.
Potential solutions:
- If Connection is a provider, is it part of the current TypeOrmModule?
- If Connection is exported from a separate @Module, is that module imported within TypeOrmModule?
@Module({
imports: [ /* the Module containing Connection */ ]
})
我想在多个应用程序之间共享实体、存储库和服务,并且我希望有一种方法可以链接我本地的更改而不是发布它们。
I've hit a dead end on trying to use multiple packages in my app with the npm link command.
I have a core project ( which produce an artifact published to npm ) and an app project which is using the core lib. Everything is fine when using the lib from npm.
Using the npm link/yarn link on my local doesn't work. I've played for 2 days with different configurations ( putting everything on the import/providers/export ) but I couldn't find a way to have the app starting with the link approach.
The core module
@Module({
imports: [
TypeOrmModule.forFeature([CountryRepository])
],
providers: [
CountryService
],
exports: [
TypeOrmModule.forFeature([CountryRepository]),
CountryService
],
})
The app module
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: '',
password: '',
database: '',
entities: [Country]
}),
CoreModule
],
controllers: [
CountryController,
],
providers: [
CountryService,
]
})
export class AppModule {
}
This is the error I'm getting when the app starts with the link created
[Nest] 12072 - 02/25/2022, 11:30:10 PM ERROR [ExceptionHandler] Nest can't resolve dependencies of the CountryRepository (?). Please make sure that the argument Connection at index [0] is available in the TypeOrmModule context.
Potential solutions:
- If Connection is a provider, is it part of the current TypeOrmModule?
- If Connection is exported from a separate @Module, is that module imported within TypeOrmModule?
@Module({
imports: [ /* the Module containing Connection */ ]
})
I want to share entities, repositories and services across multiple apps and I would like to have a way to link the changes in my local and not publish them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好像评论中提到的 Micael Levi 解决这个问题的唯一方法是使用 nohoist 功能来防止提升包@nestjs/core。据我所知,npm 目前不支持。但是,我使用的解决方法是打包我的软件包并从 .tgz 文件安装它。
Seems like as Micael Levi mentioned in the comments the only way to fix this is to use the nohoist feature to prevent hoisting the package @nestjs/core. As far as I know npm doesnt support this for now. However, a workaround I use is to pack my package and install it from the .tgz file.