Skip to content

Commit 7384ca8

Browse files
chrisyipsilverwind
authored andcommitted
module: remove '' from Module.globalPaths
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]>
1 parent a7d7463 commit 7384ca8

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

lib/module.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,9 @@ Module._initPaths = function() {
489489

490490
var nodePath = process.env['NODE_PATH'];
491491
if (nodePath) {
492-
paths = nodePath.split(path.delimiter).concat(paths);
492+
paths = nodePath.split(path.delimiter).filter(function(path) {
493+
return !!path;
494+
}).concat(paths);
493495
}
494496

495497
modulePaths = paths;

test/parallel/test-module-globalpaths-nodepath.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@ var module = require('module');
66
var isWindows = process.platform === 'win32';
77

88
var partA, partB;
9+
var partC = '';
910

1011
if (isWindows) {
1112
partA = 'C:\\Users\\Rocko Artischocko\\AppData\\Roaming\\npm';
1213
partB = 'C:\\Program Files (x86)\\nodejs\\';
13-
process.env['NODE_PATH'] = partA + ';' + partB;
14+
process.env['NODE_PATH'] = partA + ';' + partB + ';' + partC;
1415
} else {
1516
partA = '/usr/test/lib/node_modules';
1617
partB = '/usr/test/lib/node';
17-
process.env['NODE_PATH'] = partA + ':' + partB;
18+
process.env['NODE_PATH'] = partA + ':' + partB + ':' + partC;
1819
}
1920

2021
module._initPaths();
2122

2223
assert.ok(module.globalPaths.indexOf(partA) !== -1);
2324
assert.ok(module.globalPaths.indexOf(partB) !== -1);
25+
assert.ok(module.globalPaths.indexOf(partC) === -1);
2426

25-
assert.ok(Array.isArray(module.globalPaths));
27+
assert.ok(Array.isArray(module.globalPaths));

0 commit comments

Comments
 (0)