Modules are sections of code within a file that carry out a particular operation when called by a larger application. A module can be a single function or collection of related functions.
Modules enable you to divide up your code into more manageable parts. They are commonly used to divide software applications into chunks that are responsible for a single or a few related tasks.
- Use the export keyword to make a local variable, method, class, or object available to other modules.
- It’s released to the public as a freely used code.
- The developer uses the import keyword to access the public code from another module.
Environment agnostic modules
Modules shimmed per environment
Modules that are shimmed per environment provide just one application programming interface (API) but several implementations depending on the environment. Superagent, socket.io, debug, and PubNub are all good examples of shimmed per environment modules.
Modules that are environment-independent function flawlessly in every setting. Shimmed modules, however, require the surrounding environment to choose the appropriate implementation. When an application utilizes the module, this does not present a problem. The environment in which the application will operate, such as the server or client, is obvious. But the proper implementation cannot be taken if the module is being used by another module that’s already in use.
An example of this is a library that performs currency conversions by sending network queries to a REST API. A library that uses an appropriate debug library is another good example. The module that does the consumption will also distribute implementations for the various environments. This causes an effect known as cascade, which forces all modules farther down the chain to make builds for multiple settings.