Node.js listen to module loading

With the RequireJS requirement on the interface, we can listen when modules are loaded into the runtime module cache using:

requirejs.onResourceLoad = function (context, map, depArray) { console.log('onResourceLoad>>>', 'map.id:', map.id, 'context:', context); }; 

Can this be done with Node.js? It will be useful for debugging. Especially when servers upload different files (or in a different order) based on configuration.

I assume this can be documented in

https://nodejs.org/api/modules.html

but i don't see anything

+5
source share
3 answers

If you look at the source code for require() , you will find the following:

 Module._load = function(request, parent, isMain) { if (parent) { debug('Module._load REQUEST %s parent: %s', request, parent.id); } 

This shows that you can use the debug() call to get the necessary information. To do this, you will notice that the module is configured using util.debuglog('module') . This means that you need to run the application with the variable NODE_DEBUG set to module . You can do this from the console:

 NODE_DEBUG=module node main.js 

This will record what you are looking for.

+3
source

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') // You can use the module here, async or sync. 

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. }) 
+1
source

I don't know the documented callback API for module load callbacks ( although the registration mechanism for loading the module seems to exist ).

Here's a quick workaround for the apparent lack of such a callback by monkeypatching Module._load :

 const Module = require('module'); const originalModuleLoad = Module._load; Module._load = function() { originalModuleLoad.apply(this, arguments); console.log("Loaded with arguments " + JSON.stringify(arguments)); } 

I executed the above code in REPL and then did require('assert') . Here it is:

 > require('assert') Loading with arguments {"0":"assert","1":{"id":"<repl>","exports":{},"filename":null,"loaded":false,"children":[],"paths":["/Users/mz2/Projects/manuscripts-endnote-promo/repl/node_modules","/Users/mz2/Projects/manuscripts-endnote-promo/node_modules","/Users/mz2/Projects/node_modules","/Users/mz2/node_modules","/Users/node_modules","/Users/mz2/.nvm-fish/v6.1.0/lib/node_modules","/Users/mz2/.node_modules","/Users/mz2/.node_libraries","/Users/mz2/.nvm-fish/v6.1.0/lib/node"]},"2":false} 

Please do not think about using the code as described above for anything other than debugging purposes only.

+1
source

Source: https://habr.com/ru/post/1258237/


All Articles