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