Skip to content

Commit 98e5b0a

Browse files
committed
[Fix] error code MODULE_NOT_FOUND instead of ENOTDIR
Fixes #121.
1 parent 26369cf commit 98e5b0a

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

lib/async.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ module.exports = function resolve(x, options, callback) {
2020

2121
var isFile = opts.isFile || function (file, cb) {
2222
fs.stat(file, function (err, stat) {
23-
if (err && err.code === 'ENOENT') cb(null, false);
24-
else if (err) cb(err);
25-
else cb(null, stat.isFile() || stat.isFIFO());
23+
if (!err) {
24+
return cb(null, stat.isFile() || stat.isFIFO());
25+
}
26+
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
27+
return cb(err);
2628
});
2729
};
2830
var readFile = opts.readFile || fs.readFile;

lib/sync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function (x, options) {
1313
try {
1414
var stat = fs.statSync(file);
1515
} catch (e) {
16-
if (e && e.code === 'ENOENT') return false;
16+
if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
1717
throw e;
1818
}
1919
return stat.isFile() || stat.isFIFO();

test/resolver.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,34 @@ test('#25: node modules with the same name as node stdlib modules', function (t)
278278
t.equal(res, path.join(resolverDir, 'node_modules/punycode/index.js'));
279279
});
280280
});
281+
282+
test('async: #121 - treating an existing file as a dir when no basedir', function (t) {
283+
var testFile = path.basename(__filename);
284+
285+
t.test('sanity check', function (st) {
286+
st.plan(1);
287+
resolve('./' + testFile, function (err, res, pkg) {
288+
if (err) t.fail(err);
289+
st.equal(res, __filename, 'sanity check');
290+
});
291+
});
292+
293+
t.test('with a fake directory', function (st) {
294+
st.plan(4);
295+
296+
resolve('./' + testFile + '/blah', function (err, res, pkg) {
297+
st.ok(err, 'there is an error');
298+
st.notOk(res, 'no result');
299+
300+
st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
301+
st.equal(
302+
err && err.message,
303+
'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
304+
'can not find nonexistent module'
305+
);
306+
st.end();
307+
});
308+
});
309+
310+
t.end();
311+
});

test/resolver_sync.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,36 @@ test('#79 - re-throw non ENOENT errors from stat', function (t) {
204204
t.end();
205205
});
206206

207+
test('sync: #121 - treating an existing file as a dir when no basedir', function (t) {
208+
var testFile = path.basename(__filename);
209+
210+
t.test('sanity check', function (st) {
211+
st.equal(
212+
resolve.sync('./' + testFile),
213+
__filename,
214+
'sanity check'
215+
);
216+
st.end();
217+
});
218+
219+
t.test('with a fake directory', function (st) {
220+
function run() { return resolve.sync('./' + testFile + '/blah'); }
221+
222+
st.throws(run, 'throws an error');
223+
224+
try {
225+
run();
226+
} catch (e) {
227+
st.equal(e.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
228+
st.equal(
229+
e.message,
230+
'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
231+
'can not find nonexistent module'
232+
);
233+
}
234+
235+
st.end();
236+
});
237+
238+
t.end();
239+
});

0 commit comments

Comments
 (0)