Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

283
Vistas
How to access method of Project A into Project B in nodejs, i am using nestjs

i have MailEvent class and want to use SendEmail method in client project. i use NPM link to share code for client project. but i can't access this method. how can i access this method in client project. i am using nestJS framework. thanks

@Injectable()
export class MailEvent {
  constructor(private eventEmitter: EventEmitter2) {}

  // store email details to RabbitMQ
  SendEmail() {
    amqplib.connect(
      'amqp://localhost',
      (connectionError, connection) => {
        if (connectionError) {
          throw connectionError;
        }
        connection.createChannel((channelError, channel) => {
          if (channelError) {
            throw channelError;
          }
          const queue = 'Test_emails_queue';
          channel.assertQueue(queue, { durable: false });
          channel.sendToQueue(queue, Buffer.from(JSON.stringify(email)));
          // create event for sending email
          this.eventEmitter.emit('send_email', email);
        });
      },
    );
  }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In nestjs we can create a reusable module. Then, this module can be imported to another module. If the module is stored in separated repo, then we can build the module first before installing it to another repo using NPM.

Steps to do:

  1. generate nestjs resource, e.g. MailEventModule on the terminal. It will also create the controller and service class

nest g resource mail-event

  1. export the service via the module file mail-event.module.ts

    import { Module } from '@nestjs/common';
    import { MailEventService } from './mail-event.service';
    
    @Module({
      providers: [MailEventService],
      exports: [MailEventService]
    })
    export class MailEventModule { }

  2. Build the module. It will create a new folder named DIST containing the module ready to be installed in another repo/project

npm run build

  1. upload the mail-event module to npm
  2. install mail-event in the client project using npm command, e.g. npm install mail-event
  3. Import the module to the client project in client-project.module.ts

import { Module } from '@nestjs/common';
import { MailEventModule } from 'mail-event';

@Module({
  imports: [MailEventModule]
})
export class ClientProjectModule { }

  1. Add MailEventService as dependency to the client project service in client-project.service.ts

import { Injectable } from '@nestjs/common';
import { MailEventService } from 'mail-event';

@Injectable()
export class ClientProjectService {

    constructor(private mailEventService: MailEventService) { }
    
    myFunction() {
      this.mailEventService.sendEmail();
    }
}

If you have tried this steps, please give us the feedback whether it works or not. Thank you

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda