What is the syntax to export a function from a module in Node.js 1

What is the syntax to export a function from a module in Node.js

function foo() {}
function bar() {}

// To export above functions:
module.exports = foo;
module.exports = bar;

// And in the file you want to use these functions,
// import them like this:
const foo = require('./module/path');
const bar = require('./module/path');

Here is what the above code is Doing:
1. We have two functions, foo and bar.
2. We export them using module.exports.
3. We import them in another file using require.

The problem with this approach is that we can only export one function per file.

Similar Posts