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');
Answer from mash on Stack OverflowYou 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');
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
Exporting multiple functions in a module
Nodejs - how group and export multiple functions in a separate file?
javascript - Exporting multiple functions with arguments
node.js - NodeJS Module Exports: Export multiple functions vs Exporting a single function - Code Review Stack Exchange
Videos
Hello I was just wondering if any of you guys could help me with this problem Im having. I have a module.js which has 2 functions MyModule and MyModules. I would like to output both of these functions on a console but if keeps giving me an error:
let foo = moduleTest.testModule(); ^
TypeError: moduleTest.testModule is not a function
I am able to get one function to output to the console but not both.
Module.js:
function myModule(){ let foo = []; foo.testModule = function(){ foo.push('one'); foo.push('two'); } return foo; } module.exports = myModule;
function myModules(){
let foos = [];
foos.testModules = function(){
foos.push('one');
foos.push('two');
}
return foos;
}module.exports = myModules;
module-import.js:( here is where I try and output both functions)
let MyModule = require('./module');
let moduleTest = new MyModule();
let foo = moduleTest.testModule();
console.log('foo is ' + moduleTest);
//importing the second method
var MyModules = MyModuleos.MyModules;
let moduleTests = new MyModules();
let foos = moduleTests.testModules();
console.log('foo is ' + moduleTests);
For some reason it is not working for me. Any help that you guys could offer is greatly appreciated.
This would be my solution for your exporting problem! And don't mix es5 exports with es6 imports, that can get very weird - sometimes!
export const example1 = async () => {
return 'example 1'
}
export const example2 = async () => {
return 'example 2'
}
// other file
import { example1, example2 } from '../../example'
return example1()
Nevertheless if you have to mix them, just let me know! We can find a solution for this aswell!
More about exporting modules and what can go wrong!
MDN Exports and the a short story about the state of javascript modules
Below I have shared a way to declare the exporting functions in 2 different ways. Hope it helps understand the different ways in which it could be solved.
"use strict";
// utils.js
const ex1 = function() {
console.log('ex1');
};
function ex2(context) {
console.log('ex2');
};
module.exports = { example1: ex1, example2: ex2 };
You could invoke them in another (external) JS file (ex: app.js) as follows:
// app.js
const utils = require('./utils');
utils.example1(); // logs 'ex1'
utils.example2(); // logs 'ex2'
You're making it more complex than needed. Once you have your functions defined, you can export it with this:
module.exports = {
initClass,
getInstance
}
To use it, you do this:
const init = require("./class.js");
init.initClass(params);
const instance = init.getInstance();
What you're exporting from the module is an object (which I've named init in the example above) that contains two functions. You don't have to pass arguments to the functions at the time you require.
module.exports is basically an object with keys which can refer to any variables/functions of your file.In your case,
module.exports.initClass = function (params){
...
}
module.exports.getInstance = function (){
}
When importing it
var init = require('./class.') // init object has two keys -> initClass and getInstance
init.initClass('abc')
init.getInstance()
function A{}
function B{}
Export-ModuleMember -Function A
// What about B?