NestJS - Nest 无法解析依赖项 - 使用 npm/yarn 链接时

Edd*_*rut 4 npm-link typeorm nestjs

我在尝试通过 npm link 命令在我的应用程序中使用多个包时遇到了死胡同。我有一个核心项目(它生成发布到 npm 的工件)和一个使用核心库的应用程序项目。使用 npm 中的库时一切都很好。

在我本地使用 npm 链接/yarn 链接不起作用。我已经使用不同的配置玩了两天(将所有内容都放在 import/providers/export 上),但我找不到一种方法让应用程序从链接方法开始。

核心模块

    @Module({
        imports: [
            TypeOrmModule.forFeature([CountryRepository])
        ],
        providers: [
            CountryService
        ],
        exports: [
            TypeOrmModule.forFeature([CountryRepository]),
            CountryService
        ],
    })
Run Code Online (Sandbox Code Playgroud)

应用程序模块

    @Module({
        imports: [
            TypeOrmModule.forRoot({
                type: 'postgres',
                host: 'localhost',
                port: 5432,
                username: '',
                password: '',
                database: '',
                entities: [Country]
            }),
            CoreModule
        ],
        controllers: [
            CountryController,
        ],
        providers: [
            CountryService,
        ]
    })
    export class AppModule {
    }

Run Code Online (Sandbox Code Playgroud)

这是应用程序通过创建的链接启动时出现的错误

[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 */ ]
  })
Run Code Online (Sandbox Code Playgroud)

我想在多个应用程序之间共享实体、存储库和服务,并且我希望有一种方法可以链接本地中的更改而不是发布它们。

小智 5

似乎正如Micael Levi在评论中提到的那样,解决此问题的唯一方法是使用 nohoist 功能来防止提升包 @nestjs/core。据我所知 npm目前不支持这个。但是,我使用的解决方法是打包我的软件包并从 .tgz 文件安装它。

"package-name": "file:path-to-generated-tgz-file",
Run Code Online (Sandbox Code Playgroud)