question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't understand modules. Service imports don't work.

See original GitHub issue

I get that I should ask on StackOverflow, but it’s a weird place where none of my questions ever get answered, aside from myself.

The documentation is lacking for someone like me, mentally challenged.

I cannot get any of this to work either… app.module.ts

@Module({
  imports: [
    HttpModule,
    AuthenticationModule,
    UserModule,
  ],
  controllers: [AppController],
})
export class AppModule {}

user.module.ts

@Module({
  providers: [UserService],
  exports: [UserService],
})
export class UserModule {}

auth.module.ts

@Module({
  imports: [
    HttpModule,
    UserModule,
    PassportModule,
    JwtModule.register({ secret: process.env.JWT_SECRET }),
  ],
  providers: [AuthenticationService, LocalStrategy],
  controllers: [AuthenticationController],
  exports: [AuthenticationService],
})
export class AuthenticationModule {}

auth.service.ts

@Injectable()
export class AuthenticationService implements IAuthenticationService {

  constructor(
    protected readonly userService: IUserService,
    protected readonly jwtService: JwtService,
  ) {}


  /**
   * Attempts to authenticate a user with a given email and password combination.
   * 
   * @param email 
   * @param password 
   */
  public async authenticate(email: string, password: string): Promise<Session> {
    try {
      const user = await this.userService.getAuthenticationDetails(email);

      if (isNil(user)) {
        throw new Error(NOT_FOUND_ERR);
      }

      if (!comparePassword(password, user.password, user.salt)) {
        throw new Error(MISMATCH_ERR);
      }

      return sessionFactory(user._id.toString(), user.email, user.userType);
    } catch (err) {
      throw err;
    } 
  }

  /**
   * Creates a JWT from a session
   * 
   * @param session Session object to produce the JWT out of
   */
  public signJWT(session: ISession): string {
    return this.jwtService.sign(session);
  }
}

Now, when I try to run npm start I get this:

Nest can't resolve dependencies of the AuthenticationService (?, JwtService). Please make sure that the argument at index [0] is available in the AuthenticationModule context. +16ms

The docs say that I just need to import the module. Yet nothing is working. So are the docs wrong? What do I do here? Do I need to set authentication module providers to have UserService in there? If so, what’s the point of modules in that case if I can dump EVERYTHING into the main app.module.ts instead? Because this seems to be way easier.

Edit:

is the auth.module.ts supposed to look like this then?

@Module({
  imports: [
    HttpModule,
    UserModule,
    PassportModule,
    JwtModule.register({ secret: process.env.JWT_SECRET }),
  ],
  // Added User Service bellow at index [1]
  providers: [AuthenticationService, UserService, LocalStrategy],
  controllers: [AuthenticationController],
  exports: [AuthenticationService],
})
export class AuthenticationModule {}

Doesn’t it beat the purpose of modules?

Edit 2:

Well that didn’t work.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
BrunnerLiviocommented, Aug 27, 2019

@TheImpressionist more on the interface vs class thing.

From the docs:

We could determine the DTO schema by using TypeScript interfaces, or by simple classes. Interestingly, we recommend using classes here. Why? Classes are part of the JavaScript ES6 standard, and therefore they are preserved as real entities in the compiled JavaScript. On the other hand, since TypeScript interfaces are removed during the transpilation, Nest can’t refer to them at runtime.

Plus further elaboration #1228

tl;dr: Not a Nest problem, it is a TS/JS problem because interfaces do not exist on run-time.

1reaction
DogAndHerDudecommented, Aug 27, 2019

Apparently the constructor needs to look like THIS:

  constructor(
    protected readonly userService: UserService,
    protected readonly jwtService: JwtService,
  ) {}

Instead of:

  constructor(
    protected readonly userService: IUserService,
    protected readonly jwtService: JwtService,
  ) {}

Means I cannot have interfaces in the constructor. Weird flex, but ok.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Can't understand importing node modules
The problem is, when I open index.html in the browser I get this error: Uncaught TypeError: Error solving the module “@flatten- ...
Read more >
Avoiding common confusions with modules in Angular
So a lazy-loaded module that imports that shared module makes its own copy of the service. So we know that Angular creates its...
Read more >
Singleton services
Only the root AppModule should import the GreetingModule . If a lazy-loaded module imports it too, the application can generate multiple instances of...
Read more >
Angular Modules and NgModule - Complete Guide
In this post, we are going to do an introduction to Angular Modularity (the NgModule functionality) and understand why it enables several ...
Read more >
Export and Import
Technically, we may have both default and named exports in a single module, but in practice people usually don't mix them. A module...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found