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

150
Vistas
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 Respuestas
Responde la pregunta

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 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