Skip to content

Commit

Permalink
module: remove '' from Module.globalPaths
Browse files Browse the repository at this point in the history
If `$NODE_PATH` contains trailing separators, `Module.globalPaths` will
contains empty strings. When `Module` try to resolve a module's path,
`path.resolve('', 'index.js')` will boil down to `$PWD/index.js`, which
makes sub modules can access global modules and get unexpected result.

PR-URL: #1488
Reviewed-By: Roman Reiss <[email protected]>
  • Loading branch information
chrisyip authored and silverwind committed Apr 23, 2015
1 parent a7d7463 commit 7384ca8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,9 @@ Module._initPaths = function() {

var nodePath = process.env['NODE_PATH'];
if (nodePath) {
paths = nodePath.split(path.delimiter).concat(paths);
paths = nodePath.split(path.delimiter).filter(function(path) {
return !!path;
}).concat(paths);
}

modulePaths = paths;
Expand Down
8 changes: 5 additions & 3 deletions test/parallel/test-module-globalpaths-nodepath.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ var module = require('module');
var isWindows = process.platform === 'win32';

var partA, partB;
var partC = '';

if (isWindows) {
partA = 'C:\\Users\\Rocko Artischocko\\AppData\\Roaming\\npm';
partB = 'C:\\Program Files (x86)\\nodejs\\';
process.env['NODE_PATH'] = partA + ';' + partB;
process.env['NODE_PATH'] = partA + ';' + partB + ';' + partC;
} else {
partA = '/usr/test/lib/node_modules';
partB = '/usr/test/lib/node';
process.env['NODE_PATH'] = partA + ':' + partB;
process.env['NODE_PATH'] = partA + ':' + partB + ':' + partC;
}

module._initPaths();

assert.ok(module.globalPaths.indexOf(partA) !== -1);
assert.ok(module.globalPaths.indexOf(partB) !== -1);
assert.ok(module.globalPaths.indexOf(partC) === -1);

assert.ok(Array.isArray(module.globalPaths));
assert.ok(Array.isArray(module.globalPaths));

0 comments on commit 7384ca8

Please sign in to comment.