diff --git a/lib/node-modules-paths.js b/lib/node-modules-paths.js index 7c58e106..8888117d 100644 --- a/lib/node-modules-paths.js +++ b/lib/node-modules-paths.js @@ -20,6 +20,8 @@ module.exports = function (start, opts) { var parts = start.split(splitRe); var dirs = []; + var envDirs = []; + var envDelimeter = ':'; for (var i = parts.length - 1; i >= 0; i--) { if (modules.indexOf(parts[i]) !== -1) continue; dirs = dirs.concat(modules.map(function(module_dir) { @@ -31,6 +33,10 @@ module.exports = function (start, opts) { } if (process.platform === 'win32'){ dirs[dirs.length-1] = dirs[dirs.length-1].replace(":", ":\\"); + envDelimeter = ';'; } - return dirs.concat(opts.paths); + if (process.env.NODE_PATH) { + envDirs = process.env.NODE_PATH.split(envDelimeter); + } + return dirs.concat(opts.paths, envDirs); } diff --git a/test/node_path.js b/test/node_path.js index 1472bdb7..7b1f8a06 100644 --- a/test/node_path.js +++ b/test/node_path.js @@ -2,9 +2,10 @@ var path = require('path'); var test = require('tap').test; var resolve = require('../'); -test('$NODE_PATH', function (t) { - t.plan(3); - +// Ensure that the `opts.paths` paths are searched +test('opts.paths', function (t) { + t.plan(4); + resolve('aaa', { paths: [ __dirname + '/node_path/x', @@ -14,7 +15,7 @@ test('$NODE_PATH', function (t) { }, function (err, res) { t.equal(res, __dirname + '/node_path/x/aaa/index.js'); }); - + resolve('bbb', { paths: [ __dirname + '/node_path/x', @@ -24,7 +25,7 @@ test('$NODE_PATH', function (t) { }, function (err, res) { t.equal(res, __dirname + '/node_path/y/bbb/index.js'); }); - + resolve('ccc', { paths: [ __dirname + '/node_path/x', @@ -46,3 +47,71 @@ test('$NODE_PATH', function (t) { t.equal(res, path.resolve(__dirname, '..', 'node_modules/tap/lib/main.js')); }); }); + +// Ensure that the `NODE_PATH` environment is searched +test('$NODE_PATH async', function (t) { + t.plan(4); + + // Construct the NODE_PATH environment variable + var nodePaths = [ + __dirname + '/node_path/x', + __dirname + '/node_path/y' + ]; + + var delimeter = process.platform === 'win32' ? ';' : ':'; + process.env.NODE_PATH = nodePaths.join(delimeter); + + resolve('aaa', { + }, function (err, res) { + t.equal(res, __dirname + '/node_path/x/aaa/index.js'); + }); + + resolve('bbb', { + }, function (err, res) { + t.equal(res, __dirname + '/node_path/y/bbb/index.js'); + }); + + resolve('ccc', { + }, function (err, res) { + t.equal(res, __dirname + '/node_path/x/ccc/index.js'); + }); + + // ensure that relative paths still resolve against the + // regular `node_modules` correctly + resolve('tap', { + paths: [ + 'node_path', + ], + basedir: 'node_path/x', + }, function (err, res) { + t.equal(res, path.resolve(__dirname, '..', 'node_modules/tap/lib/main.js')); + }); +}); + +// Ensure that the `NODE_PATH` environment is searched in sync +test('$NODE_PATH sync', function (t) { + t.plan(4); + + // Construct the NODE_PATH environment variable + var nodePaths = [ + __dirname + '/node_path/x', + __dirname + '/node_path/y' + ]; + + var delimeter = process.platform === 'win32' ? ';' : ':'; + process.env.NODE_PATH = nodePaths.join(delimeter); + + var res = resolve.sync('aaa'); + t.equal(res, __dirname + '/node_path/x/aaa/index.js'); + + res = resolve.sync('bbb'); + t.equal(res, __dirname + '/node_path/y/bbb/index.js'); + + res = resolve.sync('ccc'); + t.equal(res, __dirname + '/node_path/x/ccc/index.js'); + + // ensure that relative paths still resolve against the + // regular `node_modules` correctly + res = resolve.sync('tap'); + t.equal(res, path.resolve(__dirname, '..', 'node_modules/tap/lib/main.js')); +});