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

发布于 2025-01-10 00:22:10 字数 1726 浏览 0 评论 0原文

我在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞旧 2025-01-17 00:22:10

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

"package-name": "file:path-to-generated-tgz-file",

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.

"package-name": "file:path-to-generated-tgz-file",
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文