How to export only one function, except other functions and import it in other file.
function messsageReceived(message) {
//print message
}
function readData(){
// reads data.
}
module.exports = mqtt_messsageReceived();
I want to use mqtt_messsageReceived in other file.
How to export only one function, except other functions and import it in other file.
function messsageReceived(message) {
//print message
}
function readData(){
// reads data.
}
module.exports = mqtt_messsageReceived();
I want to use mqtt_messsageReceived in other file.
Share Improve this question edited Aug 28, 2018 at 6:01 Ketan Yekale 2,2233 gold badges27 silver badges34 bronze badges asked Aug 28, 2018 at 5:00 codemtcodemt 4813 gold badges10 silver badges27 bronze badges 4-
2
Remove the
()
. You don[t want to invoke it, you just want to reference it. Also, you haven't definedmqtt_messsageReceived
anywhere in the code above... – CertainPerformance Commented Aug 28, 2018 at 5:01 - yes, it is actually messageReceived. Editing Typo. – codemt Commented Aug 28, 2018 at 5:02
-
module.exports = messageReceived;
would be good – NoobTW Commented Aug 28, 2018 at 5:03 -
after that, you can
var mqtt_messageReceived = require('./somefile');
and callmqtt_messageReceived
– NoobTW Commented Aug 28, 2018 at 5:04
5 Answers
Reset to default 10To export just a single function from a module:
Module file:
//function definition
function function_name(){...}
//Export
module.exports = function_name;
Import:
const function_name = require('<relative path>/module_name');
//call imported function
function_name();
To export multiple functions:
Module file:
//function definitions
function function_name1(){...}
function function_name2(){...}
//Exports
module.exports.function_name1= function_name1;
module.exports.function_name2= function_name2;
Import:
const module_name = require('<relative path>/module_name');// This returns module object with the functions from module's file.
//call imported function
module_name.function_name1();
module_name.function_name2();
What I did was declare a variable and store the functions there.
var messages = {
getMessage : function(){
},
readData : function(){
}
}
module.exports = messages;
Then I called both functions from another file and both are working.
var message = require('./message');
message.getMessage();
message.readData();
I was getting confused because now the file where the functions are won't work if I directly do node message.js
. I have to call them from another file from where I am importing them.
You can export only 1 function using the following ways
1.
module.exports = function messsageReceived(message) {
//print message
}
function readData() {
// reads data.
}
2.
function messsageReceived(message) {
//print message
}
function readData() {
// reads data.
}
module.exports = messsageReceived;
you can do it in two way :
module.exports = {
MyFunction(parameter){
console.log('export function');
}
};
Another one is :
fuction MyFunction(){
console.log('export function');
}
module.exports.MyFuntion= Myfuction;
You can export function by Using Following Code :
var messsageReceived=function(message){
// your code here
}
module.exports = {
messsageReceived: messsageReceived
}