NodeJs — Modules and require()
Node.js uses modules to organize code. You can use built-in modules like 'fs', 'path', and 'http', or create your own custom modules. The require()
function is used to import modules into your application.
// customModule.js
module.exports = {
greet: function() {
return 'Hello from custom module!';
}
};
// app.js
const custom = require('./customModule');
console.log(custom.greet());