Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nest can't resolve dependencies of the MikroOrmCoreModule (Symbol(mikro-orm-module-options), ?). Please make sure that the argument ModuleRef at index [1] is available in the MikroOrmCoreModule context. #30

Closed
hcharley opened this issue Jul 17, 2021 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@hcharley
Copy link

Describe the bug

I've tried several configurations, but I keep getting this error.

Stack trace


[Nest] 69475  - 07/17/2021, 4:01:41 PM     LOG [NestFactory] Starting Nest application...
[Nest] 69475  - 07/17/2021, 4:01:41 PM     LOG [InstanceLoader] ConfigModule dependencies initialized +188ms
[Nest] 69475  - 07/17/2021, 4:01:41 PM     LOG [InstanceLoader] DomainModule dependencies initialized +1ms
[Nest] 69475  - 07/17/2021, 4:01:41 PM     LOG [InstanceLoader] MikroOrmModule dependencies initialized +0ms
[Nest] 69475  - 07/17/2021, 4:01:41 PM   ERROR [ExceptionHandler] Nest can't resolve dependencies of the MikroOrmCoreModule (Symbol(mikro-orm-module-options), ?). Please make sure that the argument ModuleRef at index [1] is available in the MikroOrmCoreModule context.

Potential solutions:
- If ModuleRef is a provider, is it part of the current MikroOrmCoreModule?
- If ModuleRef is exported from a separate @Module, is that module imported within MikroOrmCoreModule?
  @Module({
    imports: [ /* the Module containing ModuleRef */ ]
  })

Error: Nest can't resolve dependencies of the MikroOrmCoreModule (Symbol(mikro-orm-module-options), ?). Please make sure that the argument ModuleRef at index [1] is available in the MikroOrmCoreModule context.

Potential solutions:
- If ModuleRef is a provider, is it part of the current MikroOrmCoreModule?
- If ModuleRef is exported from a separate @Module, is that module imported within MikroOrmCoreModule?
  @Module({
    imports: [ /* the Module containing ModuleRef */ ]
  })

    at Injector.lookupComponentInParentModules (/App/node_modules/@my-org/api/node_modules/@nestjs/core/injector/injector.js:189:19)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at Injector.resolveComponentInstance (/App/node_modules/@my-org/api/node_modules/@nestjs/core/injector/injector.js:145:33)
    at resolveParam (/App/node_modules/@my-org/api/node_modules/@nestjs/core/injector/injector.js:99:38)
    at async Promise.all (index 1)
    at Injector.resolveConstructorParams (/App/node_modules/@my-org/api/node_modules/@nestjs/core/injector/injector.js:114:27)
    at Injector.loadInstance (/App/node_modules/@my-org/api/node_modules/@nestjs/core/injector/injector.js:47:9)
    at Injector.loadProvider (/App/node_modules/@my-org/api/node_modules/@nestjs/core/injector/injector.js:69:9)
    at async Promise.all (index 0)
    at InstanceLoader.createInstancesOfProviders (/App/node_modules/@my-org/api/node_modules/@nestjs/core/injector/instance-loader.js:44:9)

To Reproduce

import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { ConfigModule, DatabaseModule } from '@my-org/infra';
import { ApiModule, ApiController, ApiService } from '@my-org/api';
import { Config } from '@my-org/config';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { getRootPath } from '@my-org/utils';
import { DomainModule } from '@project/domain';

import { ProjectAppController } from './app.controller';
import { ProjectAppService } from './app.service';

@Module({
  imports: [
    ConfigModule,
    DomainModule,
    GraphQLModule.forRoot({
      debug: false,
      playground: true,
      autoSchemaFile: getRootPath('apps/project-api/graphql-schema.gql'),
      path: Config.serviceGraphqlApiEndpoint,
    }),
    MikroOrmModule.forRoot(
      Config.getMikroOrmConfig({
        migrations: {
          migrationsList: [],
        },
      })
    ),
    DatabaseModule,
  ],
  controllers: [ProjectAppController, ApiController],
  providers: [ProjectAppService, ApiService],
})
export class AppModule extends ApiModule {}

DomainModule imports ResourceModule

import { Module } from '@nestjs/common';
import { ResourceService } from './resource-manifest.service';
import { ResourceResolver } from './resource-manifest.resolver';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { Resource } from './entities/resource-manifest.entity';

@Module({
  providers: [ResourceResolver, ResourceService],
  imports: [MikroOrmModule.forFeature([Resource])],
})
export class ResourceModule {}

Expected behavior

To see dependencies resolved

Additional context
Add any other context about the problem here.

Versions

Dependency Version
node v12.18.3
typescript 4.3.5
    "@mikro-orm/core": "^4.5.7",
    "@mikro-orm/migrations": "^4.5.7",
    "@mikro-orm/nestjs": "^4.2.0",
    "@mikro-orm/postgresql": "^4.5.7",
@hcharley hcharley added the bug Something isn't working label Jul 17, 2021
@hcharley
Copy link
Author

hcharley commented Jul 17, 2021

I console logged out the options I passed into the MikroOrmModule static methods I used:

forFeature [ [class Resource extends BaseEntity] ]
forRoot {
  dbName: 'project',
  user: 'project',
  host: 'localhost',
  port: 5432,
  password: undefined,
  type: 'postgresql',
  autoLoadEntities: true,
  migrations: { migrationsList: [] }
}

@evantrimboli
Copy link
Contributor

evantrimboli commented Jul 18, 2021

Looks like a dupe of: #27

@hcharley
Copy link
Author

It's a strange thing, but I was able to overcome this issue by starting my feature branch fresh. There was probably some implemenation quirk I ran into.

I'm happy to close this issue (although I may open it back up if I come across it again).

@B4nan
Copy link
Member

B4nan commented Jul 19, 2021

If you are using nest 8 then it is a dup of #27. If not, provide full repro.

@B4nan B4nan closed this as completed Jul 19, 2021
@EduVencovsky
Copy link

@hcharley what do you mean by "starting fresh" ? What exactly you did to make it work? I'm having the same issue and upgrading to v4.3 doesn't solve it.

@rynti
Copy link

rynti commented Dec 9, 2021

Preface

I encountered this error and was stuck for a while, but recently figured it out and wanted to post my issue+solution in case someone else stumbles on it.

The issue

The core issue was that I had multiple installations of nestjs, although they had the same version, but just in different folders. And then the mikro-orm packages were accessing a different installation of NestJS than my application. I think this led to the error that ModuleRef couldn't be found, because the ModuleRef that the mikro-orm modules wanted to use is from a different NestJS installation than the one that my application was actually providing.

The reason it was so hard to figure out is that I am using yarn v1 workspaces and my application is just a single package within the workspace. And my package within the workspace had (for some unknown reason) its own installation of NestJS, whereas mikro-orm was properly hoisted (hoisting means the module is installed in the root of the workspace instead of just the individual package) and installed in the root of my workspace, in addition to the aforementioned separate installation of NestJS that was also hoisted and installed in the root of my workspace.

So the structure looked like this:

/                     (root of my yarn workspace)
  node_modules/       (hoisted node modules)
    @nestjs/...       (hoisted version of @nestjs)
    @mikro-orm/...    (hoisted version of @mikro-orm packages, they access the
                       hoisted @nestjs packages from this same directory)
  packages/
    my-subpackage/    (this is my own package)
      node_modules/
        @nestjs/...   (this is a different installation of the @nestjs packages,
                       that are actually used by my application code)
      src/
        ...           (my application code)
      package.json    (the package.json for this my-subpackage)
  package.json        (the package.json for the whole yarn workspace)
  yarn.lock           (the yarn lockfile for the whole workspace)

And the confusing part was that the yarn.lock indicated that only a single version of the @nestjs/... packages were installed. Only after manually checking the directories I figured this out.

How I solved it

I could finally solve it by setting the nohoist attribute in the package.json within my-subpackage, to force yarn to not hoist the nestjs and mikro-orm packages.

So the package.json inside of my-subpackage finally looked like this:

{
  "name": "my-subpackage",
  ...
  "workspaces": {
    "nohoist": ["@mikro-orm/**", "@nestjs/**"]
  }
}

Afterwards, it was important to remove the node_modules directories and remove the existing yarn.lock file, so that yarn actually applies the nohoist rules.

Additionally, in case you encounter this error but aren't using yarn workspaces, my advice is just to go through the node_modules folders and figure out if there are any multiple versions of @nestjs/... or @mikro-orm/... packages installed, and if so, you'll have to figure out why and how to force NPM/Yarn (or whichever package manager of your choice) to only install a single version of these packages.

Good luck!

@ruscon
Copy link

ruscon commented Feb 2, 2024

for pnpm in the .npmrc need to add

public-hoist-pattern[]=@mikro-orm/*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants