Skip to content

Instantly share code, notes, and snippets.

@kaelzhang
Created August 4, 2014 02:15
Show Gist options
  • Save kaelzhang/360efb9771234284bc78 to your computer and use it in GitHub Desktop.
Save kaelzhang/360efb9771234284bc78 to your computer and use it in GitHub Desktop.
Node.js File Module
a.js
var b = require('./b')
b.b; // undefined
exports.a = 1;
b.js
var a = require('./a') -> a's module.exports
a.a; // 1
exports.b = 2;
$ node b
require(id) -> search file by id
-> file not found -> error
-> file found ->
-> module.exports loaded -> return module.exports;
-> not -> empty module.exports -> pass module.exports into file -> set flag loaded=true, run file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment