Modules
Last updated
Was this helpful?
Last updated
Was this helpful?
In the real world, a program grows organically to cope with the needs of new functionality. With growing codebase structuring and maintaining the code requires additional work. Though it will pay off in the future, it's tempting to neglect it and allow programs to be deeply tangled. In reality, it increases the complexity of the application, as one is forced to build a holistic understanding of the system and has difficulty to look any piece in isolation. Secondly, one has to invest more time in untangling to use its functionality.
Modules come to avoid these problems. A module
specifies which pieces of code it depends on, along with what functionality it provides for other modules to use. Modules that are dependent on another module are called dependencies. Various module libraries are there to organize code into modules and load it on demand.
AMD - one of the oldest module systems, initially used by .
CommonJS - module system created for Node.js server.
UMD - module system that is compatible with AMD and CommonJS.
Modules can load each other, and use special directives import
and export
to interchange functionality, and call functions of each other.
export
- labels functions and variables that should be accessible from outside the current module
import
- imports functionality from outside module
Let's see the import
, and export
mechanism in modules. We have sayHi
function exported from sayHi.js
file.
The sayHi
function is consumed in the main.js
file with the help of the import
directive.
Here, the import directive loads the module by importing the relative path and assigns the sayHi
variable.
Modules can be exported in two ways: Named and Default. Furthermore, the Named exports can be assigned inline or individually.
One can only have one default export
in a file.
Based on the type of export, we can import it in two ways. The named export are constructed using curly braces whereas, default exports are not.
While assigning modules, we should avoid circular dependency. Circular dependency is a situation where module A depends on B, and B also depends on A directly or indirectly.