Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a NativeModule function and are they cached?

require('http')
console.log(require.cache.http) // undefined

I can't find the http module in require.cache ? How can I detect it was required? I found process.moduleLoadList that contains the string NativeModule http but don't see any reference to the cached module where is it ?

like image 796
Mickael B. Avatar asked Apr 09 '26 02:04

Mickael B.


1 Answers

NativeModule means it's not cached: it's compiled. It's defined like this,

NativeModule: a minimal module system used to load the JavaScript core modules found in lib/**/*.js and deps/**/*.js. All core modules are compiled into the node binary via node_javascript.cc generated by js2c.py, so they can be loaded faster without the cost of I/O. This class makes the lib/internal/*, deps/internal/* modules and internalBinding() available by default to core modules, and lets the core modules require itself via require('internal/bootstrap/loaders') even when this file is not written in CommonJS style.

Emphasis added around "without the cost of I/O" so no caching needed.

You can find this system in lib/internal/bootstrap/loaders.js. You can find the mapping of native modules to source code here, but remember they're not read from disk. You can find js2c.py

like image 149
NO WAR WITH RUSSIA Avatar answered Apr 10 '26 16:04

NO WAR WITH RUSSIA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!