3

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.

ouflak
  • 2,458
  • 10
  • 44
  • 49

1 Answers1

5

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",
Mehran Hassani
  • 119
  • 1
  • 6
  • Thank you for the workaround, this solves the problem in the least painful way. I'll just add that the command to generate the `.tgz` file is `npm pack`. Reference: https://docs.npmjs.com/cli/v9/commands/npm-pack – Papooch Jan 24 '23 at 10:16
  • Yes exactly thanks for mentioning that! – Mehran Hassani Feb 08 '23 at 21:00