Declare multiple module.exports in Node.js Declare multiple module.exports in Node.js node.js node.js

Declare multiple module.exports in Node.js


You can do something like:

module.exports = {    method: function() {},    otherMethod: function() {},};

Or just:

exports.method = function() {};exports.otherMethod = function() {};

Then in the calling script:

const myModule = require('./myModule.js');const method = myModule.method;const otherMethod = myModule.otherMethod;// OR:const {method, otherMethod} = require('./myModule.js');


To export multiple functions you can just list them like this:

module.exports = {   function1,   function2,   function3}

And then to access them in another file:

var myFunctions = require("./lib/file.js")

And then you can call each function by calling:

myFunctions.function1myFunctions.function2myFunctions.function3


in addition to @mash answer I recommend you to always do the following:

const method = () => {   // your method logic}const otherMethod = () => {   // your method logic }module.exports = {    method,     otherMethod,    // anotherMethod};

Note here:

  • You can call method from otherMethod and you will need this a lot
  • You can quickly hide a method as private when you need
  • This is easier for most IDE's to understand and autocomplete your code ;)
  • You can also use the same technique for import:

    const {otherMethod} = require('./myModule.js');