NestJS Dependency Injection Error: provider/module is not part of currently processed module

Viewed 1644

I've just started with NestJS and I'm wondering what the best way to solve this error is

Nest cannot export a provider/module that is not a part of the currently processed module (UsersModule). Please verify whether the exported UsersService is available in this particular context.

I am following along with the documentation here and I seem to have hit a brick wall. My app won't compile so I can't test any further. I'll add some of the files as examples.

auth.module.ts

import { Module } from '@nestjs/common';
import { UsersModule } from 'src/users/users.module';
import { AuthController } from './controllers/auth/auth.controller';
import { AuthService } from './services/auth/auth.service';

@Module({
  imports: [UsersModule],
  controllers: [AuthController],
  providers: [AuthService],
  exports: [AuthService],
})
export class AuthModule {}

users.module.ts

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { UsersController } from './controllers/users/users.controller';
import { User, UserSchema } from './schemas/user.schema';
import { UsersService } from './services/users/users.service';

@Module({
  imports: [
    MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]),
  ],
  controllers: [UsersController],
  providers: [
    {
      provide: 'USERS_SERVICE',
      useClass: UsersService,
    },
  ],
  exports: [UsersService],
})
export class UsersModule {}

users.service.ts

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { CreateUserDto } from 'src/users/dto/user.dto';
import { User, UserDocument } from 'src/users/schemas/user.schema';
import { IUserService } from './user';

@Injectable()
export class UsersService implements IUserService {
  constructor(@InjectModel(User.name) private userModel: Model<UserDocument>) {}

  // async code for database modification

  }
}

Any help would be greatly appreciated! Many thanks!

1 Answers

The token of UsersService provider is the string 'UsersService' (or UsersService.name) not 'USERS_SERVICE'

So just use the short-hand syntax providers: [UsersService] or the right token in provide:.

Related