Eddie Firut
Eddie Firut

Reputation: 41

NestJS - Nest can't resolve dependencies - When using npm/yarn link

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.

Upvotes: 4

Views: 2170

Answers (1)

Mehran Hassani
Mehran Hassani

Reputation: 119

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",

Upvotes: 5

Related Questions