Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Issue #39 regarding NODE_PATH #47

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
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
8 changes: 7 additions & 1 deletion lib/node-modules-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name" : "resolve",
"description" : "resolve like require.resolve() on behalf of files asynchronously and synchronously",
"version" : "0.7.1",
"version" : "0.7.2",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if you'd rather not include this in the PR. I'm not sure how you handle your releases.

"repository" : {
"type" : "git",
"url" : "git://github.com/substack/node-resolve.git"
Expand Down
79 changes: 74 additions & 5 deletions test/node_path.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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'));
});