When you open a browser, you can request a website to be displayed by hitting a specific URL like wikipedia.com or google.com.
You don’t know how the server process your request, all you care about is that the browser loads and displays the website.
Modules are similar. You can use use a piece of code that’s written by other people without knowing the implementation detail. All you need to know is what modules are exposed for you to use, and what parameter (if any) you need to send.
The benefits of using modules
A modular approach will give your project the following features:
- Organization: Allows you to organize your code into small file chunks, with each file responsible for one job.
- Abstraction: Hides the implementation detail so you don’t need to understand how the code runs.
- Encapsulation: Just like you can expose a piece of code from a file, you can also hide / protect another piece of code from public access.
- Reusability: By exposing a piece of code that has accepts input and returns output, your code becomes a processing machine that can be reused by other pieces of code again and again.
In short, using modular approach will make your life as a developer easier.
- AMD specification, which is implemented into requireJS for client side.
- CommonJS specification, implemented into Node.js server
- UMD specification, capable of working everywhere, be it in the client, on the server or elsewhere.