私が実現しようとしているのは、複数の関数を含む 1 つのモジュールを作成することです。
モジュール.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 を宣言するにはどうすればよいでしょうか?
ベストアンサー1
次のようなことができます:
module.exports = {
method: function() {},
otherMethod: function() {},
};
あるいは単に:
exports.method = function() {};
exports.otherMethod = function() {};
次に、呼び出しスクリプトで次の操作を行います。
const myModule = require('./myModule.js');
const method = myModule.method;
const otherMethod = myModule.otherMethod;
// OR:
const {method, otherMethod} = require('./myModule.js');