7

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.

4
  • 2
    Remove the (). You don[t want to invoke it, you just want to reference it. Also, you haven't defined mqtt_messsageReceived anywhere in the code above... Commented Aug 28, 2018 at 5:01
  • yes, it is actually messageReceived. Editing Typo. Commented Aug 28, 2018 at 5:02
  • module.exports = messageReceived; would be good Commented Aug 28, 2018 at 5:03
  • after that, you can var mqtt_messageReceived = require('./somefile'); and call mqtt_messageReceived Commented Aug 28, 2018 at 5:04

5 Answers 5

12

To 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();
Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

In question, you had mentioned that you want only one function to be exported and here you are exporting all the function in messages object.
If you are calling the functions in module js itself, you shouldn't be wrapping them into object. Please check my answer.
@KetanYekale yes thank you. but I will be adding more functions later on too. and i want to call them in other places too. then I will need the help of a property. cause the code will get manageable more.
0

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;

Comments

0

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;

Comments

0

You can export function by Using Following Code :

var messsageReceived=function(message){
// your code here
}
module.exports = {
    messsageReceived: messsageReceived
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.