Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: fix module search path for preload modules #1812

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,20 @@ Module.requireRepl = function() {
return Module._load('internal/repl', '.');
};

Module._preloadModules = function(requests) {
if (!Array.isArray(requests))
return;

// Preloaded modules have a dummy parent module which is deemed to exist
// in the current working directory. This seeds the search path for
// preloaded modules.
var parent = new Module('internal/preload', null);
parent.paths = Module._nodeModulePaths(process.cwd());
requests.forEach(function(request) {
Module._load(request, parent, false);
});
};

Module._initPaths();

// backwards compatibility
Expand Down
5 changes: 1 addition & 4 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -858,10 +858,7 @@
// Load preload modules
startup.preloadModules = function() {
if (process._preload_modules) {
var Module = NativeModule.require('module');
process._preload_modules.forEach(function(module) {
Module._load(module);
});
NativeModule.require('module')._preloadModules(process._preload_modules);
}
};

Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/cluster-preload.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
var assert = require('assert');

// https://github.com/nodejs/io.js/issues/1803
// this module is used as a preload module. It should have a parent with the
// module search paths initialized from the current working directory
assert.ok(module.parent);
var expectedPaths = require('module')._nodeModulePaths(process.cwd());
assert.deepEqual(module.parent.paths, expectedPaths);

var cluster = require('cluster');
cluster.isMaster || process.exit(42 + cluster.worker.id); // +42 to distinguish
// from exit(1) for other random reasons