Home > Back-end >  How to manually get an instance of provider outside of Nest.js
How to manually get an instance of provider outside of Nest.js

Time:11-24

I have an Express.js application that I'm migrating to Nest.js, some application modules are using Nest.js, some aren't.

Let's say I have an OrderModule which is a Nest.js module and it has an OrderService in its providers and I would like to use OrderService in a product module that is not a NestJS module. Is it possible to get an instance of OrderService without converting the product module to a NestJS module?

CodePudding user response:

It's just an example for include 'apn' module (that is not nestjs native)

import { Injectable } from '@nestjs/common';
import { UserGateway } from '../../users/gateways/user.gateway';

@Injectable()
export class NotificationsGateway {
    private user: UserGateway;
    private appleapn = require('apn');
...

CodePudding user response:

The only way to get access to Nest's DI container from outside the application would be to export the app instance created by NestFactory.create() and then call app.get(OrderService) to retrieve the service.

  • Related