Skip to content

Commit c3c5b1b

Browse files
committed
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
1 parent 98649fd commit c3c5b1b

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
@@ -508,6 +508,20 @@ Module.requireRepl = function() {
508508
return Module._load('internal/repl', '.');
509509
};
510510

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

513527
// backwards compatibility

src/node.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -858,10 +858,7 @@
858858
// Load preload modules
859859
startup.preloadModules = function() {
860860
if (process._preload_modules) {
861-
var Module = NativeModule.require('module');
862-
process._preload_modules.forEach(function(module) {
863-
Module._load(module);
864-
});
861+
NativeModule.require('module')._preloadModules(process._preload_modules);
865862
}
866863
};
867864

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)