Blog>
Snippets

Exporting multiple functions

Provide an example of a module that exports several functions and document how to import a subset of them.
// module.js
// Define a set of functions to be exported
function functionA() {
    console.log('Function A called');
}

function functionB() {
    console.log('Function B called');
}

function functionC() {
    console.log('Function C called');
}

// Export the functions
module.exports = {
    functionA,
    functionB,
    functionC
};
This is the module file (module.js) where multiple functions (functionA, functionB, functionC) are defined and exported as part of an object.
// main.js
// Import only a subset of the functions from the module.js
const { functionA, functionC } = require('./module');

// Use the imported functions
functionA();
functionC();
In a separate file (main.js), a subset of the exported functions is imported from the module.js using object destructuring, and then those functions are called.