Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

278
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda