Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use only dependency injection container from Nest.js framework?

Tags:

Is it possible to use just DI and IoC functionality from the Nest.js framework? If it is, how to achieve that?

I tried to implement it in this way:

import { NestFactory } from "@nestjs/core";
import { Module, Injectable } from "@nestjs/common";

@Injectable()
class AppRepository {
  sayHi() {
    console.log("app repository");
    console.log("Hello");
  }
}

@Injectable()
class AppService {
  constructor(private appRepository: AppRepository) {}
  sayHi() {
    console.log("app service");
    this.appRepository.sayHi();
  }
}

@Module({
  imports: [],
  providers: [AppService, AppRepository]
})
class AppModule {
  constructor(private appService: AppService) {}
  sayHi() {
    console.log("app module");
    this.appService.sayHi();
  }
}

async function bootstrap() {
  const app = await NestFactory.createApplicationContext(AppModule);
  const module = app.get<AppModule>(AppModule);
  module.sayHi();
}

bootstrap();

But when I run the code I get:

app module
(node:70976) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'sayHi' of undefined
    at AppModule.sayHi (/Users/jakub/projects/nest-di-clean/build/main.js:47:25)
    at /Users/jakub/projects/nest-di-clean/build/main.js:60:16
    at Generator.next (<anonymous>)
    at fulfilled (/Users/jakub/projects/nest-di-clean/build/main.js:11:58)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

So I get instance of AppModule, but an instance of AppService isn't injected.

I would like to use it in a library I'm contributing to which doesn't need controllers and other server-side stuff.

like image 720
jakubkoci Avatar asked Feb 01 '20 12:02

jakubkoci


1 Answers

I found a solution. I didn't create a new project with nest-cli but just created an empty npm project with TypeScript and added @nestjs/common and @nestjs/core as dependencies (I want to minimize dependencies). My mistake was in tsconfig.json file where I forgot to allow emitDecoratorMetadata. Here is my package.json and tsconfig.json file for those who would be interested:

package.json

{
  "name": "nest-di-clean",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "compile": "yarn tsc",
    "start": "node ./build/main.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@nestjs/common": "^6.11.5",
    "@nestjs/core": "^6.11.5",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^6.5.4"
  },
  "devDependencies": {
    "@types/node": "^13.7.0",
    "typescript": "^3.7.5"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2015",                     
    "module": "commonjs",                   
    "outDir": "./build",                    
    "strict": true,                         
    "esModuleInterop": true,                
    "experimentalDecorators": true,         
    "emitDecoratorMetadata": true,          
    "forceConsistentCasingInFileNames": true
  }
}

The content of main.js file is all in my question.

like image 169
jakubkoci Avatar answered Sep 20 '22 22:09

jakubkoci