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

468
Visualizações
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;

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

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");
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