Modules in Node.js are reusable pieces of code that can be exported from one program and imported for use in another program.
module.exportsexports the module for use in another program.
require()imports the module for use in the current program.
ES6 introduced a more flexible, easier syntax to export modules:
- default exports use
- named exports use the
exportkeyword to export data in variables.
- named exports can be aliased with the
importis a keyword that imports any object, function, or data type.