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 Overflow
๐ŸŒ
Atomizedobjects
atomizedobjects.com โ€บ blog โ€บ javascript โ€บ how-to-export-multiple-functions-in-javascript
How to export multiple functions in JavaScript | Atomized Objects
August 27, 2021 - To export multiple functions in JavaScript, the easiest way to do so is by using the export statement before each function you want to export. As long as you do not export functions as default then you can export as many functions as you like from a module.
Discussions

Nodejs - how group and export multiple functions in a separate file?
How can I group and export multiple functions in nodejs? I am trying to group all my util functions in utils.js: async function example1 () { return 'example 1' } async function example2 () ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Exporting multiple functions in a module
You're trying to set module.exports to two different values. What you want to do is instead export an object with your two functions as props: Module.exports = { myModule, myModules, }; Then you'll be able to import them separately or invoke as props. More on reddit.com
๐ŸŒ r/node
3
2
May 11, 2018
javascript - Exporting multiple functions with arguments
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 ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
ES6 export default with multiple functions referring to each other
Below is the correct way to export multiple functions. Simply import this file like import varName from "./fileName.js" More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Statements โ€บ export
export - JavaScript - MDN Web Docs
You can have multiple named exports per module but only one default export. Each type corresponds to one of the above syntax. Named exports: js ยท // export features declared elsewhere export { myFunction2, myVariable2 }; // export individual features (can export var, let, // const, function, ...
๐ŸŒ
Softwareshorts
softwareshorts.com โ€บ how-to-export-multiple-functions-in-javascript
How to export multiple functions in JavaScript | SoftwareShorts
1import { add, multiply, divide } from 'mathHelpers'; exporting a default function is slightly different because even though you can have as many exports as you like, you can only have a single default export. 1// mathHelpers.js 2 3export default function add(a, b) { 4 return a + b; 5} 6 7export ...
๐ŸŒ
Codingem
codingem.com โ€บ home โ€บ how to export multiple functions in javascript
How to Export Multiple Functions in JavaScript - codingem.com
July 10, 2025 - To import multiple functions from a module in JavaScript, specify an object with the function names you want to import. For instance, given you export functions fun1, and fun2 from a file called example.js, you can import them into another in ...
๐ŸŒ
Ronald James
ronaldjamesgroup.com โ€บ article โ€บ exporting-multiple-items-from-a-module
Exporting Multiple Items From A Module | Ronald James
For example, a simple mathematical utility module. It might have functions like add , subtract , multiply , divide , etc. and we canโ€™t keep creating modules for each function. The first thing I am going to do is to rename the count.js into utils.js , as in utilities.
Find elsewhere
๐ŸŒ
YouTube
youtube.com โ€บ watch
Exporting Multiple JavaScript Functions from a Node Module - YouTube
We are going to build on our last tutorial and look at how you can export more than one item from a node module. This is done using an object.For more resour...
Published ย  January 30, 2019
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-export-multiple-values-or-elements-in-a-module
How to export multiple values or elements in a Module ? - GeeksforGeeks
June 18, 2024 - Node.js uses the CommonJS module system, where each file is treated as a separate module. export let element1 export const someValue export function foo(){...} Note: As we are exporting multiple values, while importing those values it is mandatory ...
๐ŸŒ
Reddit
reddit.com โ€บ r/node โ€บ exporting multiple functions in a module
r/node on Reddit: Exporting multiple functions in a module
May 11, 2018 -

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.

Top answer
1 of 4
227

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.

2 of 4
31

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';
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ react-export-multiple-functions
Export multiple Functions or Components from file in React | bobbyhadz
April 7, 2024 - Use named exports to export multiple functions in React, e.g. `export function A() {}` and `export function B() {}`.
๐ŸŒ
SitePoint
sitepoint.com โ€บ blog โ€บ javascript โ€บ understanding module.exports and exports in node.js
Understanding module.exports and exports in Node.js โ€” SitePoint
January 29, 2024 - Then in the index.js file, weโ€™re importing this function and executing it. Also notice that in the require statement, the module name is prefixed with ./, as itโ€™s a local file. Also note that thereโ€™s no need to add the file extension. We can export multiple methods and values in the same way:
Top answer
1 of 1
1

Which one is a better approach to setting up variables in NodeJS? (faster, easier to maintain, not an antipattern)

Evaluating the merit of different options depends upon these factors:

  1. How many total options are there now?
  2. How likely are you to add more options as time goes?
  3. Does the caller always set all the options at once? Or is there value to being able to set just some options
  4. Does the caller usually just set one option?

Evaluating each of these:

Total number of options

First off, if you have lots of variables, then neither A or B are particularly practical. A is not very practical because you have to make a separate API for every variable when they're all really just doing the same thing, but for a slightly different property. That's generally not a good thing in an API design.

For lots of variables, option B ends up having to list out a lot of different values all in some precise order and that's not easy to use without having the doc page open every time you want to use it and it's not all that easy to read code that's written to use it either because the reader won't remember which position every single parameter is.

Extending to add more options in the future

Both A and B require modifying the API to extend and add more options. Option A requires adding and documenting a new API entry point. Option B requires adding and documenting a parameter and then making sure that prior code that doesn't include the new parameter isn't broken (by detecting the missing parameter on the receiving end of things). Neither is ideal.

Does the caller always set all options at once

If so, option A is a pain.

Does the caller usually set just one option or a small number of options

If so, option B is a pain.

Do, what you find is that neither option A or option B is entirely flexible for different types of calling usage or extensibility or use with lots of options.

Option C

So, I'll propose using a single options object to be a lot more flexible for different types of calling usage and infinite extensibility without changing the API surface itself. This is also trivial to maintain. Let's call this option C.

// default values for options
let defaultOptions = {
    optionA: defaultValA, 
    optionB: defaultValB, 
    optionC: defaultValC, 
    optionD: defaultValD, 
    optionE: defaultValE 
};

module.exports.setOptions = function(options) {
    // copy any properties from the options object to the default options
    //   overwriting default values with new values
    Object.assign(defaultOptions, options)
}

Or, in some cases, you want to prevent adding anything to defaultOptions that isn't already there (no additional properties allowed):

module.exports.setOptions = function(options) {
    // Copy any properties from the options object to the default options
    //   overwriting default values with new values
    // Only allow copying of properties that are already in defaultOptions
    for (let prop of Object.keys(options)) {
       if (Object.hasOwnProperty(defaultOptions, prop)) {
           defaultOptions[prop] = options[prop];
       }
    }
}

And, the caller would do something like this:

m.setOptions({
    optionC: "someNewValue",
    optionE: "anotherNewValue"
});

Where they can include only whatever options they want to set and they are named options so even if there were 1000 options and you just wanted to set 2 of them, it works quite cleanly.

For extensibility, option C wins every time because it can be extended by simply documenting that you now accept another property name without changing the surface of the API at all.


Option D

A fourth option which I will call option D, is to just have one "setter" function that takes a property name and a value.

// default values for options
let defaultOptions = {
    optionA: defaultValA, 
    optionB: defaultValB, 
    optionC: defaultValC, 
    optionD: defaultValD, 
    optionE: defaultValE 
};

module.exports.setProp = function(propName, value) {
    defaultOptions[propName] = value;
    return this;
}

This allows you to have one single function that could even be chainable that can set any property.

m.setProp("optionC", "someNewValue").setProp("optionE", "anotherNewValue");

This is probably better than option A if everything is just property name and value because it's more extensible without modifying the API, but it's a pain to use if you're trying to set a lot of properties (where option C excels).


Review

For option A, it's really only practical or best if you have a small number of options (say less than 5) and the caller is typically not setting multiple options (typically only setting one option). Even then, I probably still prefer option C as its infinitely extensible without adding more entry points to the API.


For option B, it's really only practical or best if you have a small number of options (say less than 5) and the caller is always setting all the options (or at most leaving off the last one or two). Option B is complicated to use and read if there are lots of options (because you have to remember the right position in the parameter list for each option) and it's complicated to overload to only pass some options. The best part of option B is that with a small number of options, it's probably the least typing for the caller, but that lesser typing means it's less self documenting than the object with named properties in option C.


For option C, it's infinitely extensible and flexible for any type of caller use from setting one property to setting hundreds of properties. And, it's trivial to maintain, even as you add more options. This is generally my preferred choice anytime I have 3 or more options or value extensibility or flexibility in how it's used by the caller.


For option D, I don't personally find it cleaner than option C, but it is flexible and extensible, though inefficient for setting a bunch of options.

๐ŸŒ
Sabe
sabe.io โ€บ blog โ€บ javascript-export-multiple-functions
How to Export Multiple Functions in JavaScript - Sabe.io
June 24, 2022 - That's it, now they can be used just like any other function. JAVASCRIPTimport { add, subtract } from './math.js'; const result = add(1, 2); console.log(result); ... In this post, we learned how to export multiple functions in JavaScript.
๐ŸŒ
DhiWise
dhiwise.com โ€บ post โ€บ the-ultimate-guide-to-react-export-multiple-functions
React Export Multiple Functions
October 22, 2024 - To export multiple functions, simply use the export keyword before each function declaration. This allows you to maintain multiple functions within the same module without having to default to a single export.
๐ŸŒ
KnowledgeHut
knowledgehut.com โ€บ home โ€บ blog โ€บ web development โ€บ how to export node.js modules using exports
How to Export Node.js Modules Using Exports
September 13, 2023 - We use the module to export a single class/variable/function from one module to another. We use exports when we want to export multiple variables/functions from one module to another. Know more about What Are Callbacks in Node.JS.