在Node.js中声明多个module.exports

Ali*_*Ali 204 module node.js

我想要实现的是创建一个包含多个函数的模块.

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
Run Code Online (Sandbox Code Playgroud)

main.js:

var foo = require('module.js')(firstParam);
var bar = require('module.js')(secondParam);
Run Code Online (Sandbox Code Playgroud)

firstParam遇到的问题是,它是一个对象类型,secondParam是一个URL字符串,但是当我有它时,它总是抱怨类型错误.

在这种情况下,如何声明多个module.exports?

mas*_*ash 462

你可以这样做:

module.exports = {
    method: function() {},
    otherMethod: function() {}
}
Run Code Online (Sandbox Code Playgroud)

甚至只是:

exports.method = function() {};
exports.otherMethod = function() {};
Run Code Online (Sandbox Code Playgroud)

然后在调用程序中:

var MyMethods = require('./myModule.js');
var method = MyMethods.method;
var otherMethod = MyMethods.otherMethod;
Run Code Online (Sandbox Code Playgroud)

  • 总是使用`module.exports = {}`而不是`module.method = ...`.http://stackoverflow.com/a/26451885/155740 (21认同)
  • 我在这里没有使用`module.method`只有`exports.method`,它只是对`module.exports.method`的引用,所以行为方式相同.唯一的区别是我们没有定义`module.exports`,所以它默认为`{}`,除非我弄错了. (7认同)
  • @YPCrumble你可以做`var otherMethod = require('module.js').otherMethod`. (3认同)

Dev*_*rah 105

要导出多个函数,您只需将它们列出如下:

module.exports = {
   function1,
   function2,
   function3
}
Run Code Online (Sandbox Code Playgroud)

然后在另一个文件中访问它们:

var myFunctions = require("./lib/file.js")
Run Code Online (Sandbox Code Playgroud)

然后你可以通过调用来调用每个函数:

myFunctions.function1
myFunctions.function2
myFunctions.function3
Run Code Online (Sandbox Code Playgroud)

  • 您也可以在访问它们时执行以下操作:const {function1,function2,function3} = require(“ ./ lib / file.js”)`允许您直接调用它们(例如,使用function1代替myFunctions.function1 `) (2认同)

Far*_*uti 32

除@mash答案外,我建议您始终执行以下操作:

const method = () => {
   // your method logic
}

const otherMethod = () => {
   // your method logic 
}

module.exports = {
    method, 
    otherMethod,
    // anotherMethod
};
Run Code Online (Sandbox Code Playgroud)

请注意:

  • 你可以打电话methodotherMethod,你需要这么多
  • 您可以在需要时快速将方法隐藏为私有
  • 这对于大多数IDE来说更容易理解和自动完成代码;)
  • 您也可以使用相同的技术进行导入:

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

  • 请注意,这使用 es6 对象初始值设定项快捷方式 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer (3认同)
  • @Franva当您在作业的右侧执行“require(./myModule.js)”时,您实际上是将整个模块作为单个对象导入,然后当您在作业的左侧执行“const {otherMethod}”时您正在做的事情称为“解构分配”,您可以在 MDN 中阅读更多相关信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring (3认同)
  • 恕我直言,这是更好的答案,因为它解决了其他方法的访问方法。感谢您指出了这一点。 (2认同)

Ali*_*Ali 15

这仅供我参考,因为我想要实现的目标可以通过这个来实现.

在里面 module.js

我们可以做这样的事情

    module.exports = function ( firstArg, secondArg ) {

    function firstFunction ( ) { ... }

    function secondFunction ( ) { ... }

    function thirdFunction ( ) { ... }

      return { firstFunction: firstFunction, secondFunction: secondFunction,
 thirdFunction: thirdFunction };

    }
Run Code Online (Sandbox Code Playgroud)

在里面 main.js

var name = require('module')(firstArg, secondArg);
Run Code Online (Sandbox Code Playgroud)


小智 10

您可以像我下面那样使用...对于函数和箭头函数:

问候.js:

function greetFromGreet() {
  console.log("hello from greet module...");
}

const greetVar = () => {
  console.log("greet var as a arrow fn/...");
};

module.exports = { greetVar, greetFromGreet }; // ---- multiple module export...
Run Code Online (Sandbox Code Playgroud)

// ----------------------------------------------------------

应用程序.js:

const greetFromGreets = require("./greet");

greetFromGreets.greetFromGreet();
greetFromGreets.greetVar();
Run Code Online (Sandbox Code Playgroud)

// ----------------------------------------------------------


Ant*_*ley 8

module.js:

const foo = function(<params>) { ... }
const bar = function(<params>) { ... } 

//export modules
module.exports = {
    foo,
    bar 
}
Run Code Online (Sandbox Code Playgroud)

main.js:

// import modules
var { foo, bar } = require('module');

// pass your parameters
var f1 = foo(<params>);
var f2 = bar(<params>);
Run Code Online (Sandbox Code Playgroud)


小智 7

您可以这样做的一种方法是在模块中创建一个新对象,而不是替换它.

例如:

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

和打电话

var test = require('path_to_file').testOne:
testOne();
Run Code Online (Sandbox Code Playgroud)


Jon*_*kas 7

如果文件是使用ES6导出编写的,则可以编写:

module.exports = {
  ...require('./foo'),
  ...require('./bar'),
};
Run Code Online (Sandbox Code Playgroud)


Ach*_*uru 7

有多种方法可以做到这一点,下面提到一种方法。假设您有这样的 .js 文件。

let add = function (a, b) {
   console.log(a + b);
};

let sub = function (a, b) {
   console.log(a - b);
};
Run Code Online (Sandbox Code Playgroud)

您可以使用以下代码片段导出这些函数,

 module.exports.add = add;
 module.exports.sub = sub;
Run Code Online (Sandbox Code Playgroud)

您可以使用此代码片段使用导出的函数,

var add = require('./counter').add;
var sub = require('./counter').sub;

add(1,2);
sub(1,2);
Run Code Online (Sandbox Code Playgroud)

我知道这是一个迟到的回复,但希望这会有所帮助!


ick*_*fay 6

您可以编写一个在其他函数之间手动委派的函数:

module.exports = function(arg) {
    if(arg instanceof String) {
         return doStringThing.apply(this, arguments);
    }else{
         return doObjectThing.apply(this, arguments);
    }
};
Run Code Online (Sandbox Code Playgroud)


Sid*_*rth 5

用这个

(function()
{
  var exports = module.exports = {};
  exports.yourMethod =  function (success)
  {

  }
  exports.yourMethod2 =  function (success)
  {

  }


})();
Run Code Online (Sandbox Code Playgroud)


小智 5

你也可以像这样导出它

const func1 = function (){some code here}
const func2 = function (){some code here}
exports.func1 = func1;
exports.func2 = func2;
Run Code Online (Sandbox Code Playgroud)

或者像这样的匿名函数

    const func1 = ()=>{some code here}
    const func2 = ()=>{some code here}
    exports.func1 = func1;
    exports.func2 = func2;
Run Code Online (Sandbox Code Playgroud)