0

I've seen a few questions mentioning a similar error but none that require this solution.

I started writing a Nest app and initially wrote all of my logic in AppController and AppService and now want to move those to a separate module, CostsModule (and associated CostsController and CostsService).

I ran nest g module costs, nest g service costs, then nest g controller costs, and things work fine until I try to inject CostsService into CostsController's constructor. I get the following error:

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

Potential solutions:
- If Function is a provider, is it part of the current CostsModule?
- If Function is exported from a separate @Module, is that module imported within CostsModule?
  @Module({
    imports: [ /* the Module containing Function */ ]
  })

This sounds odd to me, since what I'm trying to inject is a provider, and it is part of CostsModule. It also says the argument is a Function, which makes me think it might not be referring to that specific injection, though the error goes away if I comment that line out. Code snippets:

// src/app.module.ts

@Module({
  imports: [ConfigModule.forRoot(), CostsModule],
})
export class AppModule {}
// src/costs/costs.module.ts

@Module({
  controllers: [CostsController],
  providers: [CostsService],
})
export class CostsModule {}
// src/costs/costs.controller.ts

// ... other imports omitted for brevity, but this one is problematic.
import type { CostsService } from './costs.service';

@Controller('costs')
export class CostsController {
  // If I comment out the line below, the error goes away.
  constructor(private readonly costsService: CostsService) {}

  @Put('fetch')
  updateCosts(
    @Query(
      'ids',
      new ParseArrayPipe({ items: String, separator: ',' })
    )
    ids: string[]
  ): string {
    return this.costsService.updateCosts(ids);
  }
}
// src/costs.service.ts

@Injectable()
export class CostsService {
  updateCosts(ids: string[]): string {
    return ids.join(',');
  }
}

1 Answer 1

5

This import was added automatically by VS Code, but the provider (CostsService) needs to be imported into the file (not just as a type):

import type { CostsService } from './costs.service';

Changing the import to the following fixes the issue:

import { CostsService } from './costs.service';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.