Declare multiple .exports modules in Node.js

I am trying to create a single module containing several functions in it.

module.js:

module.exports = function(firstParam) { console.log("You did it"); }, module.exports = function(secondParam) { console.log("Yes you did it"); }, // This may contain more functions 

main.js:

 var foo = require('module.js')(firstParam); var bar = require('module.js')(secondParam); 

The problem is that firstParam is an object type and secondParam is a URL string, but when I have it, it always complains that the type is incorrect.

How can I declare multiple .exports modules in this case?

+194
module
May 19 '13 at 3:07
source share
13 answers

You can do something like:

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

Or even just:

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

Then in the calling program:

 var MyMethods = require('./myModule.js'); var method = MyMethods.method; var otherMethod = MyMethods.otherMethod; 
+438
May 19 '13 at 3:11
source share

To export multiple functions, you can simply list them as follows:

 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.function1 myFunctions.function2 myFunctions.function3 
+99
Jul 03 '16 at 21:47
source share

in addition to @mash's answer, I recommend always doing the following:

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

Note:

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

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

+32
May 9 '17 at 16:23
source share

This is for my reference only, because what I tried to achieve can be achieved by this.

In module.js

We can do something like this

  module.exports = function ( firstArg, secondArg ) { function firstFunction ( ) { ... } function secondFunction ( ) { ... } function thirdFunction ( ) { ... } return { firstFunction: firstFunction, secondFunction: secondFunction, thirdFunction: thirdFunction }; } 

In main.js

 var name = require('module')(firstArg, secondArg); 
+15
May 20 '13 at 20:31
source share

One way to do this is to create a new object in the module instead of replacing it.

eg:

 var testone = function () { console.log('test one'); }; var testTwo = function () { console.log('test two'); }; module.exports.testOne = testOne; module.exports.testTwo = testTwo; 

and call

 var test = require('path_to_file').testOne: testOne(); 
+7
Mar 08 '16 at 23:04 on
source share

If the files are written using ES6 export, you can write:

 module.exports = { ...require('./foo'), ...require('./bar'), }; 
+7
Jun 05 '18 at 5:11
source share

You can write a function that manually delegates between other functions:

 module.exports = function(arg) { if(arg instanceof String) { return doStringThing.apply(this, arguments); }else{ return doObjectThing.apply(this, arguments); } }; 
+6
May 19 '13 at 3:11
source share

use this

 (function() { var exports = module.exports = {}; exports.yourMethod = function (success) { } exports.yourMethod2 = function (success) { } })(); 
+5
Jan 12 '16 at 8:08
source share

module.js:

 const foo = function(<params>) { ... } const bar = function(<params>) { ... } //export modules module.exports = { foo, bar } 

main.js:

 // import modules var { foo, bar } = require('module'); // pass your parameters var f1 = foo(<params>); var f2 = bar(<params>); 
+5
Feb 27 '19 at 5:30
source share

Two types of import and export module.

Type 1 (module.js):

 // module like a webpack config const development = { // ... }; const production = { // ... }; // export multi module.exports = [development, production]; // export single // module.exports = development; 

Type 1 (main.js):

 // import module like a webpack config const { development, production } = require("./path/to/module"); 

type 2 (module.js):

 // module function no param const module1 = () => { // ... }; // module function with param const module2 = (param1, param2) => { // ... }; // export module module.exports = { module1, module2 } 

Type 2 (main.js):

 // import module function const { module1, module2 } = require("./path/to/module"); 

How to use the import module?

 const importModule = { ...development, // ...production, // ...module1, ...module2("param1", "param2"), }; 
+2
May 6 '19 at 15:50
source share

module1.js:

 var myFunctions = { myfunc1:function(){ }, myfunc2:function(){ }, myfunc3:function(){ }, } module.exports=myFunctions; 

main.js

 var myModule = require('./module1'); myModule.myfunc1(); //calling myfunc1 from module myModule.myfunc2(); //calling myfunc2 from module myModule.myfunc3(); //calling myfunc3 from module 
+1
Mar 31 '19 at 19:59
source share

also you can export it like that

 const func1 = function (){some code here} const func2 = function (){some code here} exports.func1 = func1; exports.func2 = func2; 

or for anonymous functions like this

  const func1 = ()=>{some code here} const func2 = ()=>{some code here} exports.func1 = func1; exports.func2 = func2; 
+1
Aug 01 '19 at 19:39
source share
 module.exports = (function () { 'use strict'; var foo = function () { return { public_method: function () {} }; }; var bar = function () { return { public_method: function () {} }; }; return { module_a: foo, module_b: bar }; }()); 
0
Nov 28 '15 at 3:42 on
source share



All Articles