Home > Back-end >  Why does my NestJS library work only if I installed it from NPM?
Why does my NestJS library work only if I installed it from NPM?

Time:07-29

I'm making a NestJS library and got a very strange problem. To test my library, I created a simple NestJS project and linked my library via yarn link into it. And when I start my project it always throws me an error, and even if I delete lines of code that produce that error (even though they should not), it throws me another error and this never ends. But if I publish it and install, there are not any errors end everything's working great.

The package.json of my library:

{
    "name": "kindagoose",
    "version": "1.0.10",
    "description": "Integrate Typegoose with NestJS!",
    "main": "dist/index.js",
    "types": "dist/index.d.ts",
    "keywords": [
        "nest",
        "nestjs",
        "mongoose",
        "typegoose",
        "mongo",
        "mongodb"
    ],
    "repository": {
        "url": "https://github.com/GrapeoffJS/kindagoose",
        "type": "git"
    },
    "author": "Dmitriy Grape <[email protected]>",
    "engines": {
        "node": ">=16.16.0"
    },
    "bugs": {
        "url": "https://github.com/GrapeoffJS/kindagoose/issues",
        "email": "[email protected]"
    },
    "scripts": {
        "build": "rimraf dist && tsc -p tsconfig.json",
        "build:dev": "rimraf dist && tsc --watch -p tsconfig.json",
        "prepare": "yarn build"
    },
    "license": "GPL-3.0-only",
    "private": false,
    "devDependencies": {
        "@nestjs/common": "^9.0.7",
        "@nestjs/core": "^9.0.7",
        "@typegoose/typegoose": "^9.11.0",
        "mongoose": "^6.5.0",
        "reflect-metadata": "^0.1.13",
        "rxjs": "^7.5.6",
        "@types/mongoose": "^5.11.97",
        "@typescript-eslint/eslint-plugin": "^5.31.0",
        "@typescript-eslint/parser": "^5.31.0",
        "eslint": "^8.20.0",
        "eslint-config-prettier": "^8.5.0",
        "eslint-plugin-import": "^2.26.0",
        "eslint-plugin-prettier": "^4.2.1",
        "eslint-plugin-security": "^1.5.0",
        "eslint-plugin-simple-import-sort": "^7.0.0",
        "eslint-plugin-sonarjs": "^0.14.0",
        "eslint-plugin-unicorn": "^43.0.2",
        "prettier": "^2.7.1",
        "rimraf": "^3.0.2",
        "typescript": "^4.7.4"
    },
    "peerDependencies": {
        "@nestjs/common": "^9.0.0",
        "@nestjs/core": "^9.0.0",
        "@typegoose/typegoose": "^9.11.0",
        "mongoose": "^6.5.0",
        "reflect-metadata": "^0.1.13",
        "rxjs": "^7.5.6"
    }
}

One of errors:

ERROR [ExceptionHandler] Nest can't resolve dependencies of the KindagooseCoreModule (KindagooseConnectionName, ?). Please make sure that the argument ModuleRef at index [1] is available in the KindagooseCoreModule context.

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

Error: Nest can't resolve dependencies of the KindagooseCoreModule (KindagooseConnectionName, ?). Please make sure that the argument ModuleRef at index [1] is available in the 
KindagooseCoreModule context.

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

    at Injector.lookupComponentInParentModules (D:\WebStorm\kindagoose-test\node_modules\@nestjs\core\injector\injector.js:241:19)
    at Injector.resolveComponentInstance (D:\WebStorm\kindagoose-test\node_modules\@nestjs\core\injector\injector.js:194:33)
    at resolveParam (D:\WebStorm\kindagoose-test\node_modules\@nestjs\core\injector\injector.js:116:38)
    at async Promise.all (index 1)
    at Injector.resolveConstructorParams (D:\WebStorm\kindagoose-test\node_modules\@nestjs\core\injector\injector.js:131:27)
    at Injector.loadInstance (D:\WebStorm\kindagoose-test\node_modules\@nestjs\core\injector\injector.js:57:13)
    at Injector.loadProvider (D:\WebStorm\kindagoose-test\node_modules\@nestjs\core\injector\injector.js:84:9)
    at async Promise.all (index 0)
    at InstanceLoader.createInstancesOfProviders (D:\WebStorm\kindagoose-test\node_modules\@nestjs\core\injector\instance-loader.js:44:9)
    at D:\WebStorm\kindagoose-test\node_modules\@nestjs\core\injector\instance-loader.js:29:13

Source code: https://github.com/GrapeoffJS/kindagoose

CodePudding user response:

that's due to how nodejs module resolution works. You might have multiple @nestjs/core module loaded in your app. Search for moduleRef here: https://docs.nestjs.com/faq/common-errors

  • Related