The loading sequence for require() in Node.js
Published on: 18th Jun 2022
Overview
We are using require()
for referencing the public functions in another module. Let's do some experiment on it and see how it works.
If a script file was required in multiple script files, how many times that it will be loaded?
Let's develop a simple app that handles the customer functions and supplier functions to find it out.
-
Add a new script file called
app-cust.js
with the following lines. The purpose of this script file handles the customer related processes.// the following line should run once upon calling `required()`. console.log('this is app-cust.js');
-
Add a new script file called
app-supp.js
with the following lines. The purpose of this script file handles the supplier related processes.// the following line should run once upon calling `required()`. console.log('this is app-supp.js');
-
Add a new script file called
app-shared.js
with the following lines. It include the reference toapp-cust.js
andapp-supp.js
. This script file contains the shared functions that utilizes the customer functions and supplier functions.const cust = require('./app-cust'); const supp = require('./app-supp'); // the following line should run once upon calling `required()`. console.log('this is app-shared.js');
-
Finally, add the main entry point to this app and named it
index.js
.console.log('Review the debug log and checks the loading sequence:'); const shared = require('./app-shared'); const cust = require('./app-cust'); const supp = require('./app-supp'); console.log('done');
-
Here's the result when you run the
index.js
:Review the debug log and checks the loading sequence: this is app-cust.js this is app-supp.js this is app-shared.js done
Explanations
- In
index.js
,app-shared.js
is the first module that will be loaded into the memory. - Upon loading
app-shared.js
into the memory, it will loadapp-cust.js
and thenapp-supp.js
. But, theconsole.log('this is app-shared.js')
line will not be executed yet (it will be executed after allrequired()
has been processed). - Loads
app-cust.js
into the memory. Since this script does not reference to any other module, theconsole.log('this is app-cust.js')
line will be executed. You may be treat it like a 'startup code' in that module. - Loads
app-supp.js
into the memory. Again, since this script does not reference to any other module, theconsole.log('this is app-supp.js')
line will be executed. - Upon completed loading
app-cust.js
andapp-supp.js
,console.log('this is app-shared.js')
inapp-shared.js
will be executed. - Finally,
console.log('done')
line inindex.js
will be executed.
What if we fork the index.js or run index.js with worker?
All console.log()
lines will appear multiple times that depends on how many time you fork the index.js.
References
Conclusion
require()
is CommonJS module system. With the detail analysis on how the loading module works, it helps in writing a better startup script.
Jump to #NODEJS blog
Author
Lau Hon Wan, software developer.