Node.js standart libs auto-require
I like node.js because it's damn fast. But I also like to make a global objects in my application. I think nothing wrong with it. For example I have global.App object to keep object-wide things.
It was really annoying for me to write require('fs') or require('util') in every file, I also think it's not as fast as access global object.
So I come up with this solution:
var node = {};
var modules = ['child_process', 'fs', 'http', 'https', 'cluster',
'crypto', 'dns', 'domain', 'net', 'url', 'util', 'vm', 'path'
];
var loadedModules = {};
modules.forEach(function(moduleName) {
Object.defineProperty(node, moduleName, {
get: function() {
if (!loadedModules[moduleName]) {
loadedModules[moduleName] = require(moduleName);
}
return loadedModules[moduleName];
}
});
});
module.exports = node;
global.node = node;
It make lazy-loading for all standart libraries as getters of global object node
Later in my code:
node.fs.readFile
node.util._extend({}, params);
node.url.parse(node.url.resolve(endpoint, filepath));
node.url.format(urlObj);Written by Pavel
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Node
Authors
Did you know?
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#