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: https://github.com/iojs/io.js/pull/1488
Reviewed-By: Roman Reiss <me@silverwind.io>
pull/1511/head
Chris Yip 2015-04-21 15:13:27 +08:00 committed by Roman Reiss
parent a7d74633f2
commit 7384ca83f9
2 changed files with 8 additions and 4 deletions

View File

@ -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;

View File

@ -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));