Function as a Library


From a module we can export a module or a single function like this: // module.exports = hi;


examples/basic/lib2.js
function say_hi(name) {
    console.log('Hello ' + name)
}

console.log('Loading module')

module.exports = say_hi;

examples/basic/app2.js
const say_hi = require('./lib2')

console.log('Hello World')

say_hi('Foo')

$ node examples/basic/app2.js

Loading module
Hello World
Hello Foo