require()
is found in the code of node-modules almost as often as Array#forEach()
. The most annoying thing is that we most often connect the modules "util"
, "fs"
and "path"
, a little less often "url"
. The presence of other connected modules depends on the task of the module. Moreover, speaking of the "util"
module, it is loaded into the memory of the node-process even if you have never connected it.
demandLoad()
that demandLoad()
has been in production for half a year already. As we just did not drive it ... This is the load testing of a specific process, and the work of demandLoad()
in the worker-processes of clusters, and the work of the process under a small load for a long time. Results were compared using demandLoad()
and using require()
. No significant deviations in comparison were observed.
demandLoad()
. If anyone is interested, ask questions in the comments, take screenshots, I can tell you about the methods and tools of testing, other possibilities of using the approach. Today, as follows from the title of the article, we will get rid of those who already get bored with require()
in the caps of each node-module.
"mytestsite.com"
, and we have it here:
~/projects/mytestsite.com
~/projects/mytestsite.com/lib/bin/server.js
require()
:
console.log(util.inspect(url.parse('https://habrahabr.ru/')));
"require-all.js"
file somewhere, outside of all projects.
~/projects/general/require-all.js
global
properties. Accordingly, we can define our own global objects. So we must do with all the modules we use.
require-all.js
list of all used modules in all projects:
// "util", // .. "console". // console.log(), , // util.inspect() global.util = require('util'); // , : demandLoad(global, 'fs', 'fs'); demandLoad(global, 'path', 'path'); demandLoad(global, 'url', 'url'); // npm-, : demandLoad(global, 'express', 'express'); // , , , : demandLoad(global, 'routes', './../mytestsite.com/lib/routes'); // demandLoad function demandLoad(obj, name, modPath){ // // . }
Map
), and, for example, walk on it / her in a loop so as not to repeat a line of code with a call to demandLoad()
. You can, for example, read the list of used npm-modules from package.json
. If, for example, the number of modules used is very high, and you do not want to clutter up the global scope, you can define, for example, an empty object m
( let m = {}
), define m
in global
( global['m'] = m
), and already apply demandLoad()
to m
. As they say, to whom it is more convenient.
--require
key to the node launch (versions> = 4.x):
node --require ~/projects/general/require-all.js \ ~/projects/mytestsite.com/lib/bin/server.js
Url { protocol: 'https:', slashes: true, auth: null, host: 'habrahabr.ru', port: null, hostname: 'habrahabr.ru', hash: null, search: null, query: null, pathname: '/', path: '/', href: 'https://habrahabr.ru/' }
require-all.js
within each project separately.
node --require ~/projects/mytestsite.com/lib/require-all.js \ ~/projects/mytestsite.com/lib/bin/server.js
require-all.js
at the same time:
node --require ~/projects/general/require-all.js \ --require ~/projects/mytestsite.com/lib/require-all.js \ ~/projects/mytestsite.com/lib/bin/server.js
--require
+ global
bundle can also be used to extend / overload standard node features.
demandLoad()
is not defined in our file (1) (whence we call demandLoad()
), but in some file (2), with file (1) and file (2) located in different directories, the last parameter must pass the full path to the module, for example:
demandLoad(global, 'routes', path.join(__dirname, './../mytestsite.com/lib/routes'));
require()
that is called from demandLoad()
will search for a module relative to the folder where that same file (2) is located with the description of demandLoad()
, instead of looking for a module relative to file (1), from where we call demandLoad()
.
Source: https://habr.com/ru/post/273405/