Since node.js modules are imported (required) synchronously, just having a require statement means the module is imported.
While RequireJS can import modules asynchronously, even listening is an important function, but native is required in node.js. Thus, as you probably know:
const module = require('module')
To add to this, not only synchronization is required, but also the use of the module, it must be explicitly specified in the same file where it is used. There are several ways around this, but it is best used in every module where you use the module.
For certain modules that require async initialization, either the module must provide an event, or you can wrap the init function with a promise or callback. For example, using a promise:
const module = require('module') // Create a promise to initialize the module inside it: const initialized = new Promise((resolve, reject) => { // Init module inside the promise: module.init((error) => { if(error){ return reject(error) } // Resolve will indicate successful init: resolve() }) }) // Now with wrapped init, proceed when done: initialized .then(() => { // Module is initialized, do what you need. }) .catch(error => { // Handle init error. })
source share