Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
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 <me@silverwind.io>
  • Loading branch information
chrisyip authored and silverwind committed Apr 23, 2015
commit 7384ca83f97a28b0cecaabe879e9af0fe8631b62
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));