nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.19k stars 7.58k forks source link

[Bug]: Nest cannot resolve dependencies specified in the module (when one uses it with TypeORM?) #220

Closed rychkog closed 6 years ago

rychkog commented 6 years ago

Hi @kamilmysliwiec!

I'm currently building a project using Nest and TypeORM. But I've been experiencing a strange issue with a broken DI mechanism. It looks to me that it is somehow connected to the Nest <-> TypeORM wiring Here is the screenshot of my terminal's output

image

Below is the branch of my Nest fork, where this issue is reproducible (based on examples/05-sql-typeorm). https://github.com/nestjs/nest/compare/master...rychkog:typeorm-nest-di-resolver-issue

Brief description: when I start creating a module that contains couple repositories as providers and connection for them - DI gets broken and gives me a message that you can see on the screenshot above.

If you'll have any questions regarding the issue - feel free to contact me - I'll try to help as much as I can

kamilmysliwiec commented 6 years ago

Hi @rychkog, Let's try to export entire providers objects, for example:

exports: [{
   provide: 'PhotoRepositoryToken2',
   useFactory: (connection: Connection) => connection.getRepository(Photo2),
   inject: ['DbConnectionToken'],
}],

To avoid repeating you can create a standalone variable.

rychkog commented 6 years ago

@kamilmysliwiec Now it works like that in the example project but doesn't in my actual project... At the point where there is an import statement for the main Nest module, it just exists node process

At this point node process exits. I am totally confused ...

import { ApplicationModule } from './modules/app.module';

There are no errors at all

kamilmysliwiec commented 6 years ago

@rychkog we need more context here, please, attach some code. Also, let's try to disable modules step by steps so you won't have to paste everything here :)

rychkog commented 6 years ago

@kamilmysliwiec I understand - trying to come up with a more descriptive example

kamilmysliwiec commented 6 years ago

@rychkog awesome 🙂

rychkog commented 6 years ago

@kamilmysliwiec Good morning! :) I've spent whole evening yesterday trying to figure out what is the cause of my issue, so I describe it here in hope, that community might benefit from my pain and foolishness :)

Symptoms:

  1. Nest complains dependency injection is broken and it cannot resolve dependencies of some components or that it cannot access property provide of undefined.
  2. App gets transpiled but when started - exits unexpectedly without any errors, without any little clue in the terminal

Solutions and observations

Thanks for your support! :)

kamilmysliwiec commented 6 years ago

Hi @rychkog, True, the circular dependencies may cause dizziness 🙂 Thank you for your comprehensive explanation!

postpersonality commented 5 years ago

I had this issue too. Removing a barrel file for repositories (./repositories/index.ts) and reimporting from the files directly solved similar problem with imported item being undefined.

lock[bot] commented 5 years ago

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.