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.

Answer from skozin on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › export
export - JavaScript | MDN
After the export keyword, you can use let, const, and var declarations, as well as function or class declarations. You can also use the export { name1, name2 } syntax to export a list of names declared elsewhere.
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';
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
Export Multiple Functions of a Pytorch Module
With torch.jit.script it was possible to export multiple functions of a pytorch module by annotating the relevant functions with torch.jit.export like so: import torch class Module(torch.nn.Module): def __init__(self): super().__init__() self.network = torch.nn.Sequential(torch.nn.Identity()) ... More on discuss.pytorch.org
🌐 discuss.pytorch.org
8
1
January 4, 2024
Jest: How to export multiple functions into .spec.js test file?
You want this: module.exports = { functionOne, functionTwo }; Then import: const { functionOne, functionTwo } = require('./filepath'); And for what it's worth, this technically doesn't have anything to do with jest. More on reddit.com
🌐 r/learnjavascript
2
2
February 5, 2020
Named exports of functions vs default export object with functions inside?
Named exports. It's easier to see what's exported, and the interop with CommonJS is easier. More on reddit.com
🌐 r/node
23
39
November 25, 2021
People also ask

What are the different ways to export multiple functions in React?
You can export multiple functions using named exports or default exports.
🌐
dhiwise.com
dhiwise.com › post › the-ultimate-guide-to-react-export-multiple-functions
React Export Multiple Functions
What are some best practices for exporting multiple functions in React?
- Use descriptive names for your functions. - Group related functions together in the same file. - Avoid exporting too many functions from a single file. - Consider using named exports for clarity and reusability.
🌐
dhiwise.com
dhiwise.com › post › the-ultimate-guide-to-react-export-multiple-functions
React Export Multiple Functions
Why is it beneficial to export multiple functions from a single file in React?
Exporting multiple functions promotes code organization, reusability, and maintainability. It allows you to break down your code into smaller, more manageable units and share them across different components.
🌐
dhiwise.com
dhiwise.com › post › the-ultimate-guide-to-react-export-multiple-functions
React Export Multiple Functions
🌐
DhiWise
dhiwise.com › post › the-ultimate-guide-to-react-export-multiple-functions
React Export Multiple Functions
October 22, 2024 - A default export can be imported without curly braces and is typically used for the main functionality of the js file. In contrast, named exports allow for as many named exports as needed, providing flexibility when you have to export multiple components or functions from a single module.
🌐
Softwareshorts
softwareshorts.com › how-to-export-multiple-functions-in-javascript
How to export multiple functions in JavaScript | SoftwareShorts
1const add = (a, b) => a + b; 2const multiply = (a, b) => a * b; 3const divide = (a, b) => a / b; 4 5export { add, multiply, divide }; And lastly, if you cannot use ES6 then you can use module.exports like so:
🌐
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
If you find yourself writing lots of functions in a single file, consider splitting them up into smaller, more detailed modules. Doing this makes debugging easier, organizes your work, and helps you identify which modules to import into a new project. A module titled cleverLC101Work is not nearly as helpful as one called arraySortingMethods. ... A file containing JavaScript code intended for use in other Node programs.
Find elsewhere
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-export-and-import-multiple-functions-in-vanilla-javascript-for-the-browser-d8c706ca40e6
How to Export and Import Multiple Functions in JavaScript | by Taha Jiruwala | JavaScript in Plain English
December 19, 2024 - // main.js import { function1, function2 } from "./utils.js"; // Now you can use function1 and function2 function1(); function2(); Originally published at https://tahajiru.com. Thank you for being a part of the In Plain English community! Before you go: Be sure to clap and follow the writer ️👏️️ · Follow us: X | LinkedIn | YouTube | Discord | Newsletter | Podcast · Create a free AI-powered blog on Differ. ... Have a story to share? Join our global community of writers today! ... New JavaScript and Web Development content every day.
🌐
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 - Exporting multiple values from a Node.js module can be done in various ways, each with its own advantages. Using module.exports to export an object is the most common and flexible approach, but you can also use the exports object for individual exports or mix both methods.
🌐
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.

🌐
React
react.dev › learn › importing-and-exporting-components
Importing and Exporting Components – React
There are two primary ways to export values with JavaScript: default exports and named exports. So far, our examples have only used default exports. But you can use one or both of them in the same file.
🌐
CoreUI
coreui.io › answers › how-to-export-a-function-in-javascript
How to export a function in JavaScript · CoreUI
November 19, 2025 - This pattern provides clean module boundaries and explicit dependency management. Use the export keyword before function declarations for named exports or export default for single function exports. // Named exports - multiple functions per ...
🌐
Ronald James
ronaldjamesgroup.com › article › exporting-multiple-items-from-a-module
Exporting Multiple Items From A Module | Ronald James
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. I am also going to update the requiring declaration to include utils and not count . // We don’t need a .js as it automatically finds the JavaScript ...
🌐
W3Schools
w3schools.com › js › js_modules.asp
JavaScript Modules
Modules allow multiple developers to work on different parts of the codebase simultaneously with less risk of conflicts. Clear module boundaries enhance communication and make it easier to add new features with minimal impact on existing code. ... Variables and functions defined within a module are private by default, only becoming accessible to other modules when explicitly exported...
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Modules
JavaScript modules - JavaScript | MDN
2 weeks ago - There is also a type of export called the default export — this is designed to make it easy to have a default function provided by a module, and also helps JavaScript modules to interoperate with existing CommonJS and AMD module systems (as explained nicely in ES6 In Depth: Modules by Jason ...
🌐
Atomizedobjects
atomizedobjects.com › blog › javascript › how-to-export-multiple-functions-in-javascript
How to export multiple functions in JavaScript | Atomized Objects
August 27, 2021 - All you have to do is apply the statement before each of your function definitions and as long as you are using JavaScript with ES6 or later which most modern projects will be, then the functions will be exported. You can use the export statement as many times as you like in a single file.
🌐
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:
🌐
Sabe
sabe.io › blog › javascript-export-multiple-functions
How to Export Multiple Functions in JavaScript | Sabe
June 24, 2022 - We can now export both of these functions to be imported in another file. JAVASCRIPTconst add = (a, b) => a + b; const subtract = (a, b) => a - b; export { add, subtract };
🌐
Medium
medium.com › @vino7tech › es6-modules-importing-exporting-and-organizing-javascript-code-e869676fe372
ES6 Modules: Importing, Exporting, and Organizing JavaScript Code | by Vinotech | Medium
October 15, 2024 - Importing: You can import the exports from one module into another to use the functionality. There are two ways to export code in ES6: named exports and default exports · In named exports, you explicitly export multiple items from a module by name, and you must import them using the same name.
🌐
MakeUseOf
makeuseof.com › home › programming › how to import and export functions in javascript
How to Import and Export Functions in JavaScript
September 22, 2022 - The second method is to add the export keyword just before declaring the function. ... You can export multiple functions by using the first method. This is done by including the names of the desired functions in the curly bracket.
🌐
Reddit
reddit.com › r/learnjavascript › jest: how to export multiple functions into .spec.js test file?
r/learnjavascript on Reddit: Jest: How to export multiple functions into .spec.js test file?
February 5, 2020 -

The production code is in a file called functionOne.js. I named the first function in it functionOne as well:
function functionOne(){
// Stuff }

function functionTwo(){  
  // Stuff
}  
  
module.exports = functionOne, functionTwo;  

And the first line of the test file is

const functionOne = require('filepath');  

In the test file I use both functions and it says functionTwo is undefined. What is the correct syntax to get both functions into the test file?