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
🌐
LaunchCode
education.launchcode.org › intro-to-professional-web-dev › chapters › modules › exporting.html
13.4. Exporting Modules — Introduction to Professional Web Development in JavaScript documentation
module.exports = { isPalindrome: isPalindrome, evenOrOdd: evenOrOdd, randomArrayElement: randomArrayElement } Within the {}, we create a series of key:value pairs. The keys will be the names used in index.js to call the functions. The values are the functions themselves. ... We do not have to make the key match the name of the function, but doing so helps maintain consistency between files. ... This will NOT work, because Node expects only ONE module.exports statement in a file.
Discussions

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
node.js - NodeJS Module Exports: Export multiple functions vs Exporting a single function - Code Review Stack Exchange
To update parameters in our main.js file, which has a lot of configurable variables, we can do the following ... module.exports.setVarA = function(val){ VarA = val; } module.exports.setVarB = function(val){ VarB = val; } ...so on for VarC up to VarZ ... module.exports.setAllVariables = function(valA, valB.... valZ){ VarA = valA; VarB = valB; ... VarZ = valZ; } Which one is a better approach to setting up variables in NodeJS... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
July 25, 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
Solution for exporting async/await functions
I don't understand what problem this solves. An async function is just a function that returns a promise. You can export it the same way as any other function, then import and use it like you would use any promise-returning function. I pasted the 'current situation' example from the README into the babel playground, added export in front of it, and it seems to handle it just fine . Am I missing something? More on reddit.com
🌐 r/node
13
2
June 1, 2016
🌐
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
🌐
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.

🌐
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 ...
🌐
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, ...
Find elsewhere
🌐
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.
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.

🌐
Atomizedobjects
atomizedobjects.com › blog › javascript › how-to-export-multiple-functions-in-javascript
How to export multiple functions in JavaScript | Atomized Objects
August 27, 2021 - As you can see in this example exporting multiple functions with JavaScript is pretty easy, this type of exporting is known as named exports. When it comes to importing these functions all you have to do is reference them as if the module is an object that you need to destructure. Here is an example of how to import multiple functions in JavaScript from a single module: import { exampleFunctionOne, exampleFunctionTwo } from "./some-js-file"
🌐
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 ...
🌐
YouTube
youtube.com › watch
How to Create, Export and Import Module in Node js || How to Export Multiple Module in Node js - YouTube
In Node.js, modules allow you to organize and structure your code by dividing it into separate files. Here's a complete description of how to create, export,...
Published   January 13, 2025
🌐
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:
🌐
Softwareshorts
softwareshorts.com › how-to-export-multiple-functions-in-javascript
How to export multiple functions in JavaScript | SoftwareShorts
And lastly, if you cannot use ES6 then you can use module.exports like so: 1const add = (a, b) => a + b; 2const multiply = (a, b) => a * b; 3const divide = (a, b) => a / b; 4 5module.exports = { add, multiply, divide }; In summary, there are many ways to export multiple functions in JavaScript but all of them will make use of the export statement or module.exports.
🌐
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.
🌐
Medium
rayyanx95.medium.com › understanding-the-tricky-module-exports-and-exports-in-node-js-5f48bf515c40
Understanding the tricky module.exports and exports in Node.js | by Ibrahim AlRayyan | Medium
April 9, 2021 - A module is a separate program file in Node.js. When creating a module, it can be classified as a grouping of all related functions into a file. getYear = function() { return new Date().getFullYear(); } getMonth = function() { return new Date().getMonth(); } Keeping all functional programs in a single file is really hard to maintain and deal with during the development. Having the code split up into multiple accessible files allows us to have appropriate file names for every file.
🌐
Node.js
nodejs.org › api › modules.html
Modules: CommonJS modules | Node.js v25.9.0 Documentation
To have a module execute code multiple times, export a function, and call that function. Modules are cached based on their resolved filename. Since modules may resolve to a different filename based on the location of the calling module (loading from node_modules folders), it is not a guarantee ...
🌐
Stanley Ulili
stanleyulili.com › node › node-modules-import-and-use-functions-from-another-file
Node.js Modules: Import and use Functions from Another File
March 26, 2020 - In this tutorial, we covered how to create and export a module, import a module and went over different ways to export multiple functions and values, and also different types of modules in Node.js.