DEV Community

Discussion on: Build a NestJS Module for Knex.js (or other resource-based libraries) in 5 Minutes

Collapse
 
mtaylor769 profile image
Mike Taylor

I am getting this error from the /.constants. Why is this throwing an error for a constant?

Nest can't resolve dependencies of the NestKnexService (?). Please make sure that the argument NEST_KNEX_OPTIONS at index [0] is available in the NestKnexService context.

Potential solutions:
- If NEST_KNEX_OPTIONS is a provider, is it part of the current NestKnexService?
- If NEST_KNEX_OPTIONS is exported from a separate @Module, is that module imported within NestKnexService?
  @Module({
    imports: [ /* the Module containing NEST_KNEX_OPTIONS */ ]
  })
Enter fullscreen mode Exit fullscreen mode
Collapse
 
johnbiundo profile image
John Biundo

Hi Mike,

Can you provide some more context for this error? Do you have a repository that you can share?

Best,
John

Collapse
 
ozanulutas profile image
Ozan Ulutaş

Hi. For the future readers;
Error complains about NestKnexService can not resolve injected dependency. In this case the dependancy is NEST_KNEX_OPTIONS which is a custom provider. To make the dependency injection work, the provider needs to be registered in the knex module. NEST_KNEX_OPTIONS provider is not included in this article. It is included in github link author provided.

import { connectionFactory } from './nest-knex-connection.provider';
import { createKnexProviders } from './knex.providers';

@Global()
@Module({
  providers: [
    KnexService,
    connectionFactory,
    ...createKnexProviders({/*...knex options*/}),
  ],
  exports: [KnexService, connectionFactory],
})
export class KnexModule {}
Enter fullscreen mode Exit fullscreen mode