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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16631064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:32:19  来源:igfitidea点击:

Declare multiple module.exports in Node.js

node.jsmodule

提问by Ali

What I'm trying to achieve is to create one module that contains multiple functions in it.

我想要实现的是创建一个包含多个功能的模块。

module.js:

模块.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:

main.js:

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

The problem I have is that the firstParamis an object type and the secondParamis a URL string, but when I have that it always complains that the type is wrong.

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

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

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

回答by mash

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');

回答by Devorah

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.function1
myFunctions.function2
myFunctions.function3

回答by Fareed Alnamrouti

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

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

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

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

module.exports = {
    method, 
    otherMethod,
    // anotherMethod
};

Note here:

注意这里:

  • You can call methodfrom otherMethodand 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');

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

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

回答by Ali

This is just for my reference as what I was trying to achieve can be accomplished by this.

这仅供我参考,因为我试图实现的目标可以通过此实现。

In the module.js

在里面 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 the main.js

在里面 main.js

var name = require('module')(firstArg, secondArg);

回答by Anthony Awuley

module.js:

模块.js:

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

//export modules
module.exports = {
    foo,
    bar 
}

main.js:

主要.js:

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

// pass your parameters
var f1 = foo(<params>);
var f2 = bar(<params>);

回答by Jesse Teixeira

One way that you can do it is creating a new object in the module instead of replacing it.

一种方法是在模块中创建一个新对象而不是替换它。

for example:

例如:

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

and to call

并打电话

var test = require('path_to_file').testOne:
testOne();

回答by Jon Sakas

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

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

module.exports = {
  ...require('./foo'),
  ...require('./bar'),
};

回答by icktoofay

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

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

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

回答by Siddharth

use this

用这个

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

  }
  exports.yourMethod2 =  function (success)
  {

  }


})();

回答by illvart

Two types module import and export.

两种类型的模块导入和导出。

type 1 (module.js):

类型 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):

类型 1 (main.js):

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

type 2 (module.js):

类型 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):

类型 2 (main.js):

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

How to use import module?

如何使用导入模块?

const importModule = {
  ...development,
  // ...production,
  // ...module1,
  ...module2("param1", "param2"),
};