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

149
Views
How to export and use a function using Express?

I have two file alarm.js and notifications.js. In alarm.js I need to call a method called sendPush from notifications.js.

What I've tried :
Exporting the function from notifications.js:

module.exports.sendPush = function(params){
   console.log("sendPush from notifcations.js called");
   console.log(params);
}

Importing it in alarm.js and use it :

let helperNotif = require('./notifications')
router.post("/", async (req, res) => {
    const params = {
        param1: 'a',
        param2: 'b'
    }
    helperNotif.sendPush(params)    
});

The problem:
I keep getting the error saying helperNotif.sendPush is not a function

The question :
How can I call this notification.js sendPush function from my alarm.js file ?

[EDIT] maybe I should add that in notifications.js I have some router.get and router.post and at the end module.exports = router;

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

If your notifications.js ends with module.exports = router, that will overwrite your module.exports.sendPush = .... If you want to export both the router and the sendPush, you can write

function sendPush(params){
   console.log("sendPush from notifcations.js called");
   console.log(params);
}
...
module.exports = {router, sendPush};

To import the router elsewhere, you must then write

const {router} = require("./notifications.js");
over 4 years ago · Santiago Trujillo 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!