Search code examples
nestjsnestjs-config

Nest js dependency injection is not working


Am working on a NestJS project. The aim of this project is to list the s3 buckets. I am using aws-sdk for aws support. Here am using the code. The issue is in my AWSService.ts file

import { Injectable } from '@nestjs/common';
import { S3 } from 'aws-sdk';

@Injectable()
export class AWSService {
  constructor(private readonly s3: S3) {}

  async listBuckets(): Promise<S3.Buckets> {
    const response = await this.s3.listBuckets().promise();
    return response.Buckets || [];
  }
}

This code will return the following error

[Nest] 26352  - 08/01/2023, 3:36:41 PM     LOG [NestFactory] Starting Nest application...
[Nest] 26352  - 08/01/2023, 3:36:41 PM   ERROR [ExceptionHandler] Nest can't resolve dependencies of the AWSService (?). Please make sure that the argument function() {
        if (klass !== Object) {
          return klass.apply(this, arguments);
        }
      } at index [0] is available in the AWSModule context.

Potential solutions:
- Is AWSModule a valid NestJS module?
- If function() {
        if (klass !== Object) {
          return klass.apply(this, arguments);
        }
      } is a provider, is it part of the current AWSModule?
- If function() {
        if (klass !== Object) {
          return klass.apply(this, arguments);
        }
      } is exported from a separate @Module, is that module imported within AWSModule?
  @Module({
    imports: [ /* the Module containing function() {
        if (klass !== Object) {
          return klass.apply(this, arguments);
        }
      } */ ]
  })

Error: Nest can't resolve dependencies of the AWSService (?). Please make sure that the argument function() {
        if (klass !== Object) {
          return klass.apply(this, arguments);
        }
      } at index [0] is available in the AWSModule context.

Potential solutions:
- Is AWSModule a valid NestJS module?
- If function() {
        if (klass !== Object) {
          return klass.apply(this, arguments);
        }
      } is a provider, is it part of the current AWSModule?
- If function() {
        if (klass !== Object) {
          return klass.apply(this, arguments);
        }
      } is exported from a separate @Module, is that module imported within AWSModule?
  @Module({
    imports: [ /* the Module containing function() {
        if (klass !== Object) {
          return klass.apply(this, arguments);
        }
      } */ ]
  })

    at Injector.lookupComponentInParentModules (C:\Users\app\node_modules\@nestjs\core\injector\injector.js:247:19)
    at Injector.resolveComponentInstance (C:\Users\app\node_modules\@nestjs\core\injector\injector.js:200:33)
    at resolveParam (C:\Users\app\node_modules\@nestjs\core\injector\injector.js:120:38)
    at async Promise.all (index 0)
    at Injector.resolveConstructorParams (C:\Users\app\node_modules\@nestjs\core\injector\injector.js:135:27)
    at Injector.loadInstance (C:\Users\app\node_modules\@nestjs\core\injector\injector.js:61:13)
    at Injector.loadProvider (C:\Users\app\node_modules\@nestjs\core\injector\injector.js:88:9)
    at C:\Users\app\node_modules\@nestjs\core\injector\instance-loader.js:56:13
    at async Promise.all (index 3)
    at InstanceLoader.createInstancesOfProviders (C:\Users\app\node_modules\@nestjs\core\injector\instance-loader.js:55:9)

If i change the code like this, its working fine

private readonly s3: S3
constructor() {
    this.s3 =  new S3();
}

Actually what is the problem here? Is this is the right coding standard?

Expecting a valuable feedback

Thanks in advance.


Solution

  • looks like that S3 was not registered as a provider in your AWSModule module. Thus, nestjs doesn't knows what it is.

    You could use custom providers like this:

    1. { provide: S3, useValue: new S3() }
    2. { provide: S3, useFactory: () => new S3() } -- in this case you can inject things on it. Read the docs.