DEV Community

Elijah Logan
Elijah Logan

Posted on

Learning about Modules

Modules are reusable pieces of code that can be exported from one program and imported for use in another.
They’re useful because it makes it easier to fix, reuse, protect code. It also helps prevent of the global namespace
We define a module in a file with module.exports equal to the object being imported.
-example

Const self = aggregates.all();
Module.exports = self;

Or
Const plane = {}
Plane.parts = 4

Module.exports = plane;

To make use of the exported module and behavior defined within it you can import the module with the require function.

-example

const christianity  = require(‘self’);

Const airPort = require(‘plane’);

You can also wrap any collection of data and functions into a object and export that having it equal to module.exports.

-example

Module.exports = {
post-modernity:()

=> return skepticism,
romanticism: () => return ‘Hey Emerson,
}

You could do same with export default

Top comments (0)