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

node.js Node.js

Stafan樱

2020-03-23

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

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

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

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

第2660篇《在Node.js中声明多个module.exports》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

7个回答
西门卡卡西 2020.03.23

您也可以使用这种方法

module.exports.func1 = ...
module.exports.func2 = ...

要么

exports.func1 = ...
exports.func2 = ...
Mandy村村 2020.03.23

如果在模块文件而不是简单对象中声明一个类

文件:UserModule.js

//User Module    
class User {
  constructor(){
    //enter code here
  }
  create(params){
    //enter code here
  }
}
class UserInfo {
  constructor(){
    //enter code here
  }
  getUser(userId){
    //enter code here
    return user;
  }
}

// export multi
module.exports = [User, UserInfo];

主文件:index.js

// import module like
const { User, UserInfo } = require("./path/to/UserModule");
User.create(params);
UserInfo.getUser(userId);
卡卡西 2020.03.23

用这个

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

  }
  exports.yourMethod2 =  function (success)
  {

  }


})();
达蒙 2020.03.23

你也可以像这样导出它

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

或像这样的匿名函数

    const func1 = ()=>{some code here}
    const func2 = ()=>{some code here}
    exports.func1 = func1;
    exports.func2 = func2;
Jim西门 2020.03.23

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

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

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

类型1(module.js):

// module like a webpack config
const development = {
  // ...
};
const production = {
  // ...
};

// export multi
module.exports = [development, production];
// export single
// module.exports = development;

类型1(main.js):

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

类型2(module.js):

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

// export module
module.exports = {
  module1,
  module2
}

类型2(main.js):

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

如何使用导入模块?

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

要导出多个功能,您可以像这样列出它们:

module.exports = {
   function1,
   function2,
   function3
}

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

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

然后,您可以通过调用以下命令来调用每个函数:

myFunctions.function1
myFunctions.function2
myFunctions.function3

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android