Skip to content

Commit 5759722

Browse files
ofrobotsFishrock123
authored andcommitted
src: fix module search path for preload modules
When the preload module is not a abs/relative path, we should use the standard search mechanism of looking into the node_modules folders outwards. The current working directory is deemed to be the 'requiring module', i.e. parent. The search path starts from cwd outwards. Fixes: nodejs#1803 PR-URL: nodejs#1812 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent 53e98cc commit 5759722

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

lib/module.js

+14
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,20 @@ Module.requireRepl = function() {
503503
return Module._load('internal/repl', '.');
504504
};
505505

506+
Module._preloadModules = function(requests) {
507+
if (!Array.isArray(requests))
508+
return;
509+
510+
// Preloaded modules have a dummy parent module which is deemed to exist
511+
// in the current working directory. This seeds the search path for
512+
// preloaded modules.
513+
var parent = new Module('internal/preload', null);
514+
parent.paths = Module._nodeModulePaths(process.cwd());
515+
requests.forEach(function(request) {
516+
Module._load(request, parent, false);
517+
});
518+
};
519+
506520
Module._initPaths();
507521

508522
// backwards compatibility

src/node.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -839,10 +839,7 @@
839839
// Load preload modules
840840
startup.preloadModules = function() {
841841
if (process._preload_modules) {
842-
var Module = NativeModule.require('module');
843-
process._preload_modules.forEach(function(module) {
844-
Module._load(module);
845-
});
842+
NativeModule.require('module')._preloadModules(process._preload_modules);
846843
}
847844
};
848845

test/fixtures/cluster-preload.js

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
var assert = require('assert');
2+
3+
// https://github.com/nodejs/io.js/issues/1803
4+
// this module is used as a preload module. It should have a parent with the
5+
// module search paths initialized from the current working directory
6+
assert.ok(module.parent);
7+
var expectedPaths = require('module')._nodeModulePaths(process.cwd());
8+
assert.deepEqual(module.parent.paths, expectedPaths);
9+
110
var cluster = require('cluster');
211
cluster.isMaster || process.exit(42 + cluster.worker.id); // +42 to distinguish
312
// from exit(1) for other random reasons

0 commit comments

Comments
 (0)