Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 4 additions & 3 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports = function resolve (x, opts, cb) {

var extensions = opts.extensions || [ '.js' ];
var y = opts.basedir || path.dirname(caller());

var parent = opts.filename || y;

opts.paths = opts.paths || [];

if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\\/])/.test(x)) {
Expand All @@ -39,15 +40,15 @@ module.exports = function resolve (x, opts, cb) {
else loadAsDirectory(path.resolve(y, x), function (err, d, pkg) {
if (err) cb(err)
else if (d) cb(null, d, pkg)
else cb(new Error("Cannot find module '" + x + "' from '" + y + "'"))
else cb(new Error("Cannot find module '" + x + "' from '" + parent + "'"))
})
});
}
else loadNodeModules(x, y, function (err, n, pkg) {
if (err) cb(err)
else if (n) cb(null, n, pkg)
else if (core[x]) return cb(null, x);
else cb(new Error("Cannot find module '" + x + "' from '" + y + "'"))
else cb(new Error("Cannot find module '" + x + "' from '" + parent + "'"))
});

function loadAsFile (x, pkg, cb) {
Expand Down
7 changes: 4 additions & 3 deletions lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = function (x, opts) {

var extensions = opts.extensions || [ '.js' ];
var y = opts.basedir || path.dirname(caller());
var parent = opts.filename || y;

opts.paths = opts.paths || [];

Expand All @@ -29,9 +30,9 @@ module.exports = function (x, opts) {
}

if (core[x]) return x;
throw new Error("Cannot find module '" + x + "' from '" + y + "'");

throw new Error("Cannot find module '" + x + "' from '" + parent + "'");

function loadAsFileSync (x) {
if (isFile(x)) {
return x;
Expand Down
15 changes: 13 additions & 2 deletions test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var test = require('tap').test;
var resolve = require('../');

test('async foo', function (t) {
t.plan(9);
t.plan(10);
var dir = __dirname + '/resolver';

resolve('./foo', { basedir : dir }, function (err, res, pkg) {
Expand Down Expand Up @@ -33,6 +33,11 @@ test('async foo', function (t) {
resolve('foo', { basedir : dir }, function (err) {
t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
});

// Test that filename is reported as the "from" value when passed.
resolve('foo', { basedir : dir, filename : path.join(dir, 'baz.js') }, function (err) {
t.equal(err.message, "Cannot find module 'foo' from '" + path.join(dir, 'baz.js') + "'");
});
});

test('bar', function (t) {
Expand Down Expand Up @@ -175,7 +180,7 @@ test('normalize', function (t) {
});

test('cup', function (t) {
t.plan(3);
t.plan(4);
var dir = __dirname + '/resolver';

resolve('./cup', { basedir : dir, extensions : [ '.js', '.coffee' ] },
Expand All @@ -193,6 +198,12 @@ test('cup', function (t) {
function (err, res) {
t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'");
});

// Test that filename is reported as the "from" value when passed.
resolve('./cup', { basedir : dir, extensions : [ '.js' ], filename : path.join(dir, 'cupboard.js') },
function (err, res) {
t.equal(err.message, "Cannot find module './cup' from '" + path.join(dir, 'cupboard.js') + "'");
});
});

test('mug', function (t) {
Expand Down
15 changes: 14 additions & 1 deletion test/resolver_sync.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var test = require('tap').test;
var resolve = require('../');
var path = require('path');

test('foo', function (t) {
var dir = __dirname + '/resolver';
Expand All @@ -17,7 +18,19 @@ test('foo', function (t) {
t.throws(function () {
resolve.sync('foo', { basedir : dir });
});


// Test that filename is reported as the "from" value when passed.
t.throws(
function () {
resolve.sync('foo', { basedir : dir, filename : path.join(dir, 'bar.js') });
},
{
name : 'Error',
message : "Cannot find module 'foo' from '" +
path.join(dir, 'bar.js') + "'"
}
);

t.end();
});

Expand Down