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
Nodejs - how group and export multiple functions in a separate file?
Exporting multiple functions in a module
javascript - Exporting multiple functions with arguments
ES6 export default with multiple functions referring to each other
Videos
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'
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.
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()
The export default {...} construction is just a shortcut for something like this:
const funcs = {
foo() { console.log('foo') },
bar() { console.log('bar') },
baz() { foo(); bar() }
}
export default funcs
It must become obvious now that there are no foo, bar or baz functions in the module's scope. But there is an object named funcs (though in reality it has no name) that contains these functions as its properties and which will become the module's default export.
So, to fix your code, re-write it without using the shortcut and refer to foo and bar as properties of funcs:
const funcs = {
foo() { console.log('foo') },
bar() { console.log('bar') },
baz() { funcs.foo(); funcs.bar() } // here is the fix
}
export default funcs
Another option is to use this keyword to refer to funcs object without having to declare it explicitly, as @pawel has pointed out.
Yet another option (and the one which I generally prefer) is to declare these functions in the module scope. This allows to refer to them directly:
function foo() { console.log('foo') }
function bar() { console.log('bar') }
function baz() { foo(); bar() }
export default {foo, bar, baz}
And if you want the convenience of default export and ability to import items individually, you can also export all functions individually:
// util.js
export function foo() { console.log('foo') }
export function bar() { console.log('bar') }
export function baz() { foo(); bar() }
export default {foo, bar, baz}
// a.js, using default export
import util from './util'
util.foo()
// b.js, using named exports
import {bar} from './util'
bar()
Or, as @loganfsmyth suggested, you can do without default export and just use import * as util from './util' to get all named exports in one object.
One alternative is to change up your module. Generally if you are exporting an object with a bunch of functions on it, it's easier to export a bunch of named functions, e.g.
export function foo() { console.log('foo') },
export function bar() { console.log('bar') },
export function baz() { foo(); bar() }
In this case you are export all of the functions with names, so you could do
import * as fns from './foo';
to get an object with properties for each function instead of the import you'd use for your first example:
import fns from './foo';