Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!