Which modules does Node.js 'require' automatically?
javascript, node.js
Solution
Starting in Node 0.8, `repl.js` defines a list of built-in libraries that will be automatically required when you type their name on the REPL:
exports._builtinLibs = ['assert', 'buffer', 'child_process', 'cluster',
'crypto', 'dgram', 'dns', 'events', 'fs', 'http', 'https', 'net',
'os', 'path', 'punycode', 'querystring', 'readline', 'repl',
'string_decoder', 'tls', 'tty', 'url', 'util', 'vm', 'zlib'];
...
if (exports._builtinLibs.indexOf(cmd) !== -1) {
var lib = require(cmd);
if (cmd in self.context && lib !== self.context[cmd]) {
self.outputStream.write('A different "' + cmd +
'" already exists globally\n');
} else {
self.context._ = self.context[cmd] = lib;
self.outputStream.write(self.writer(lib) + '\n');
}
self.displayPrompt();
return;
}
This is specifically a function of `repl.js`, and does not work at all in any way when writing your own Node.js programs; there, you must specifically `require` anything you want to use.
Problem
Quick question, but one I surprisingly couldn't find the answer for with a bit of Googling... when I use the `node` interactive interpreter, I can type in the names of basically all core modules and see them output as objects onto the console... however, I understand that the core modules aren't all included like this by default when running a .js file. Can anyone clear this up for me? All help appreciated.