Nest: Unable to resolve dependencies error

Created on 12 Jul 2018  路  8Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior


My issue is almost exactly the same as the one reported here: #360
I have an AuthModule and an ActorModule.

AuthModule:

import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { HttpStrategy } from './http.strategy';
import { AuthController } from './auth.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ActorToken } from './actor_token.entity';
import { ActorModule } from 'actor/actor.module';

@Module({
    imports: [TypeOrmModule.forFeature([ActorToken]), ActorModule],
    controllers: [AuthController],
    providers: [AuthService, HttpStrategy],
    exports: [AuthModule],
})
export class AuthModule {}

ActorModule:

import { Module, Global } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ActorService } from './actor.service';
import { ActorController } from './actor.controller';
import { Actor } from './actor.entity';

@Module({
    imports: [TypeOrmModule.forFeature([Actor])],
    controllers: [ActorController],
    providers: [ActorService],
    exports: [ActorModule],
})
export class ActorModule {}

Where I am getting an error is here:

@Controller('auth')
export class AuthController {

    constructor(
        private readonly authService: AuthService,
        private readonly actorService: ActorService,
    ) {}

The log (pasted below) indicates that I am not injecting ActorService correctly.

> [email protected] start /Users/apple/Desktop/company/projects/strata/starta-hub
> ts-node -r tsconfig-paths/register src/main.ts

[Nest] 6630   - 2018-7-12 14:15:54   [NestFactory] Starting Nest application...
[Nest] 6630   - 2018-7-12 14:15:54   [InstanceLoader] TypeOrmModule dependencies initialized +387ms
[Nest] 6630   - 2018-7-12 14:15:56   [InstanceLoader] TypeOrmCoreModule dependencies initialized +2123ms
[Nest] 6630   - 2018-7-12 14:15:56   [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 6630   - 2018-7-12 14:15:56   [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 6630   - 2018-7-12 14:15:56   [InstanceLoader] AppModule dependencies initialized +1ms
[Nest] 6630   - 2018-7-12 14:15:56   [InstanceLoader] ActorModule dependencies initialized +2ms
[Nest] 6630   - 2018-7-12 14:15:56   [ExceptionHandler] Nest can't resolve dependencies of the AuthController (+, ?). Please make sure that the argument at index [1] is available in the current context.
Error: Nest can't resolve dependencies of the AuthController (+, ?). Please make sure that the argument at index [1] is available in the current context.
    at Injector.lookupComponentInExports (/Users/apple/Desktop/company/projects/strata/starta-hub/node_modules/@nestjs/core/injector/injector.js:129:19)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)
 1: node::Abort() [/Users/apple/.nvm/versions/node/v8.4.0/bin/node]
 2: node::Chdir(v8::FunctionCallbackInfo<v8::Value> const&) [/Users/apple/.nvm/versions/node/v8.4.0/bin/node]
 3: v8::internal::FunctionCallbackArguments::Call(void (*)(v8::FunctionCallbackInfo<v8::Value> const&)) [/Users/apple/.nvm/versions/node/v8.4.0/bin/node]
 4: v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<false>(v8::internal::Isolate*, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::FunctionTemplateInfo>, v8::internal::Handle<v8::internal::Object>, v8::internal::BuiltinArguments) [/Users/apple/.nvm/versions/node/v8.4.0/bin/node]
 5: v8::internal::Builtin_Impl_HandleApiCall(v8::internal::BuiltinArguments, v8::internal::Isolate*) [/Users/apple/.nvm/versions/node/v8.4.0/bin/node]
 6: 0x30f0240840dd
 7: 0x30f0241735e9
Abort trap: 6

I have been through various related issues and I am still not sure what I am doing incorrectly (I have only just started experimenting with NestJS and I can't wait to get a hold of it because it looks like an amazing framework).

Expected behavior

The dependency gets correctly resolved.

Minimal reproduction of the problem with instructions

You can find the project linked here: https://github.com/pdubey/nestJSProject.

I think the issue can be reproduced simply by npm run start in the project directory.

Environment


Nest version: 5.1.0


For Tooling issues:
- Node version:   v8.4.0
- Platform:   Mac 10.12.3

Others:

Most helpful comment

You should export your actorService from your actorModule

All 8 comments

You should export your actorService from your actorModule

Wow. Thanks, it worked. I am not sure why though. I would really appreciate it if you could explain it to me.

You should export your actorService from your actorModule

A module is like a blackbox, you have to export the injectables from the module that you want to be available in another module when you import it. That鈥檚 it :)

Thank you so much :)

@adrien2p Thank you man, its working fine. And I have one more doubt in nestjs currently

what about

@Injectable({
providedIn: 'root',
})

?????

Please let me explain about this in nestjs

I didn鈥檛 understand your question sorry, could you explain what you need ?

@sulthanmamusa

Statement from Kamil (framework core maintainer):

In terms of the providedIn, there is no plan to implement it in the future. Basically, we just don't need that on the backend side, since dead code elimination (aka tree-shaking) is useless for us.

From #1019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings