0
0
Node.jsframework~10 mins

CommonJS require and module.exports in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - CommonJS require and module.exports
Start: File A wants code from File B
File A calls require('File B')
Node reads File B
Node executes File B
File B sets module.exports
Node returns module.exports to File A
File A uses imported code
End
This flow shows how one file asks for code from another using require, Node runs the second file, and returns what it exports.
Execution Sample
Node.js
/* fileB.js */
module.exports = function() {
  return 'Hello from B';
};

/* fileA.js */
const greet = require('./fileB');
console.log(greet());
File A imports a function from File B using require and then calls it to print a message.
Execution Table
StepActionFilemodule.exports ValueResult/Output
1File A starts and calls require('./fileB')fileA.jsundefinedNo output yet
2Node reads and executes fileB.jsfileB.jsfunction() { return 'Hello from B'; }No output yet
3fileB.js finishes, module.exports set to functionfileB.jsfunction() { return 'Hello from B'; }No output yet
4require returns exported function to fileA.jsfileA.jsfunction() { return 'Hello from B'; }No output yet
5fileA.js calls the imported functionfileA.jsfunction() { return 'Hello from B'; }Output: Hello from B
6Program endsfileA.jsfunction() { return 'Hello from B'; }Output complete
💡 Program ends after fileA.js calls the imported function and prints the output.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
module.exports in fileB.jsundefinedundefinedfunction() { return 'Hello from B'; }function() { return 'Hello from B'; }function() { return 'Hello from B'; }function() { return 'Hello from B'; }
greet in fileA.jsundefinedundefinedundefinedfunction() { return 'Hello from B'; }function() { return 'Hello from B'; }function() { return 'Hello from B'; }
Key Moments - 3 Insights
Why does require return the value of module.exports and not other variables?
Because Node.js specifically returns the module.exports object from the required file, ignoring other local variables. See step 4 in the execution_table where require returns module.exports.
What happens if module.exports is not set in the required file?
If module.exports is not set, it defaults to an empty object {}. So require returns {}. This is why setting module.exports is important to export your code. See step 3 where module.exports is assigned.
Can require execute code in the required file?
Yes, require runs the entire file once to set up module.exports. This is why any code outside functions runs immediately. See step 2 where Node executes fileB.js.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of module.exports in fileB.js after step 3?
AA function that returns 'Hello from B'
Bundefined
CAn empty object {}
DA string 'Hello from B'
💡 Hint
Check the 'module.exports Value' column at step 3 in the execution_table.
At which step does fileA.js receive the exported function from fileB.js?
AStep 2
BStep 5
CStep 4
DStep 1
💡 Hint
Look for when require returns the exported value in the execution_table.
If fileB.js did not set module.exports, what would require('./fileB') return in fileA.js?
Anull
BAn empty object {}
Cundefined
DA function
💡 Hint
Recall the default value of module.exports if not set, as explained in key_moments.
Concept Snapshot
CommonJS modules use module.exports to share code.
Use require('file') to import exports.
Node runs the required file once.
require returns module.exports from that file.
module.exports defaults to {} if not set.
This pattern helps organize Node.js code.
Full Transcript
In Node.js CommonJS modules, one file can use require to import code from another file. When require is called, Node reads and runs the required file. That file sets module.exports to the value it wants to share. Then require returns that module.exports value to the caller. The caller can then use the imported code. If module.exports is not set, it defaults to an empty object. This process allows files to share functions, objects, or values in a clear way.