diff --git a/lib/async.js b/lib/async.js index 99695612..e23e1374 100644 --- a/lib/async.js +++ b/lib/async.js @@ -32,22 +32,23 @@ module.exports = function resolve(x, options, callback) { var readFile = opts.readFile || fs.readFile; var extensions = opts.extensions || ['.js']; - var basedir = opts.basedir || path.dirname(caller()); + var y = opts.basedir || path.dirname(caller()); + var parent = opts.filename || y; opts.paths = opts.paths || []; if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) { - var res = path.resolve(basedir, x); + var res = path.resolve(parent, x); if (x === '..' || x.slice(-1) === '/') res += '/'; - if (/\/$/.test(x) && res === basedir) { + if (/\/$/.test(x) && res === parent) { loadAsDirectory(res, opts.package, onfile); } else loadAsFile(res, opts.package, onfile); - } else loadNodeModules(x, basedir, function (err, n, pkg) { + } else loadNodeModules(x, parent, function (err, n, pkg) { if (err) cb(err); else if (core[x]) return cb(null, x); else if (n) return cb(null, n, pkg); else { - var moduleError = new Error("Cannot find module '" + x + "' from '" + basedir + "'"); + var moduleError = new Error("Cannot find module '" + x + "' from '" + parent + "'"); moduleError.code = 'MODULE_NOT_FOUND'; cb(moduleError); } @@ -60,7 +61,7 @@ module.exports = function resolve(x, options, callback) { if (err) cb(err); else if (d) cb(null, d, pkg); else { - var moduleError = new Error("Cannot find module '" + x + "' from '" + basedir + "'"); + var moduleError = new Error("Cannot find module '" + x + "' from '" + parent + "'"); moduleError.code = 'MODULE_NOT_FOUND'; cb(moduleError); } diff --git a/lib/sync.js b/lib/sync.js index e987b888..272594ee 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -23,25 +23,26 @@ module.exports = function (x, options) { var readFileSync = opts.readFileSync || fs.readFileSync; var extensions = opts.extensions || ['.js']; - var basedir = opts.basedir || path.dirname(caller()); + var y = opts.basedir || path.dirname(caller()); + var parent = opts.filename || y; opts.paths = opts.paths || []; if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) { - var res = path.resolve(basedir, x); + var res = path.resolve(parent, x); if (x === '..' || x.slice(-1) === '/') res += '/'; var m = loadAsFileSync(res) || loadAsDirectorySync(res); if (m) return m; } else if (core[x]) { return x; } else { - var n = loadNodeModulesSync(x, basedir); + var n = loadNodeModulesSync(x, parent); if (n) return n; } if (core[x]) return x; - var err = new Error("Cannot find module '" + x + "' from '" + basedir + "'"); + var err = new Error("Cannot find module '" + x + "' from '" + parent + "'"); err.code = 'MODULE_NOT_FOUND'; throw err; diff --git a/test/resolver.js b/test/resolver.js index 116bb242..0092bc7a 100644 --- a/test/resolver.js +++ b/test/resolver.js @@ -3,7 +3,7 @@ var test = require('tape'); var resolve = require('../'); test('async foo', function (t) { - t.plan(10); + t.plan(11); var dir = path.join(__dirname, 'resolver'); resolve('./foo', { basedir: dir }, function (err, res, pkg) { @@ -34,6 +34,11 @@ test('async foo', function (t) { t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'"); t.equal(err.code, 'MODULE_NOT_FOUND'); }); + + // 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) { @@ -176,7 +181,7 @@ test('normalize', function (t) { }); test('cup', function (t) { - t.plan(4); + t.plan(5); var dir = path.join(__dirname, 'resolver'); resolve('./cup', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) { @@ -193,6 +198,12 @@ test('cup', function (t) { t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'"); t.equal(err.code, 'MODULE_NOT_FOUND'); }); + + // 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) { diff --git a/test/resolver_sync.js b/test/resolver_sync.js index 5b8057fe..99cb4fcb 100644 --- a/test/resolver_sync.js +++ b/test/resolver_sync.js @@ -19,6 +19,18 @@ test('foo', function (t) { 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(); });