#Module injection issue

19 messages · Page 1 of 1 (latest)

icy anchor
#

Hello, I need your help guys if possible because I have a Nestjs module that works perfectly fine in my project but I need to export it in a package. Is it a way to make it work since I have this issue when I import the module from my package :

ERROR [ExceptionHandler] UndefinedDependencyException [Error]: Nest can't resolve dependencies of the PersonService (?, PersonRepository, CivilityRepository, FamilySituationRepository, CityRepository, CountryRepository, EmploymentStatusRepository). Please make sure that the argument dependency at index [0] is available in the PersonModule context.

Potential solutions:
- Is PersonModule a valid NestJS module?
- If dependency is a provider, is it part of the current PersonModule?
- If dependency is exported from a separate @Module, is that module imported within PersonModule?
  @Module({
    imports: [ /* the Module containing dependency */ ]
  })
hollow panther
#

impossible to investigate without some code samples

icy anchor
#

Do you have a working example please ? I'm not allowed to share it sadly 😦

It's just that in my case, it perfectly work on the api but when I just copy it, as it is, on a package whitout changing anything and then import it back on api, it prints this error..

unique umbra
#

When it says "argument dependency" without actually printing the name, it usually means, the reference is undefined at the time of bootsrtap. This is usually caused by circular references between files.

icy anchor
#

If I export all the modules in index.ts of the package, is it wrong ? I felt like it was one of the issues I had

#

I have also some issues on the Datasource who cannot be find

#

If you have some good examples, it could be really cool. I spent days trying everything in order to make it work and it's making me late on my work 😦

#

I also need to export all the files of the packages, not only the .module.ts file. Can it be wrong in some ways ?

light thistle
icy anchor
#

"circular file imports - where a.ts. import b.ts which imports a.ts" Does that means that in my situation, I have an issue because I put all the module files are all in index.ts of the package ?

#

Can I import all the modules files into the package's main index.ts ? Will it not make this issue happens ?

unique umbra
#

You're talking about issues with datasource.. does the package import datasource from the application? There's your circular import right there

icy anchor
unique umbra
#

You build a dynamic module which receives the data surce as an argument from the outside. This is how most library modules should be designed

icy anchor
#

Okay thx, i'll try it later or tomorrow morning. I appreciate your help

unique umbra
#

in simplest terms (an example of dynamic module that receives dataSource externally):

interface LibraryModuleOptions {
  imports: any[]
  dataSourceToken: any
}


@Module({})
export class LibraryModule {
  forRootAsync(options: LibraryModuleOptions): DynamicModule {
    return {
      module: LibraryModule,
      impports: options.imports
      providers: [
        {
           provide: 'some-provider-that-needs-datasource',
           inject: [options.dataSourceToken],
           useFactory: (dataSource: DataSource) => {
              // ... do whatever you need with the datasource, return a provider instance
           }
        }
      ]
      exports: ['some-provider-that-needs-datasource']
    }
  }
}
icy anchor
#

Just to be sure, ATM I have this in the package which needs the Datasource :

@Injectable()
export class HistoryRepository extends RepositoryStarter<History> {
    constructor(datasource: DataSource) {
        ...
    }
}

And in the *.module.ts file :

@Module({
    imports: [TypeOrmModule.forFeature([History, ...]), ...],
    exports: [HistoryService, HistoryRepository],
    controllers: [HistoryController],
    providers: [HistoryService, HistoryRepository],
})
export class HistoryModule {}
zealous shard
#

@icy anchor from where DataSource comes from?

icy anchor
#

The API who imports the package