Skip to content

Commit

Permalink
[Fix] throw an error for an invalid explicit main with a missing `.…
Browse files Browse the repository at this point in the history
…/index.js` file

Fixes #223. Fixes #143.
  • Loading branch information
Cheesetouched authored and ljharb committed Nov 29, 2020
1 parent 561e223 commit 4bece07
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 4 deletions.
8 changes: 7 additions & 1 deletion lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,13 @@ module.exports = function resolve(x, options, callback) {
loadAsDirectory(dir, pkg, function (err, n, pkg) {
if (err) return cb(err);
if (n) return cb(null, n, pkg);
loadAsFile(path.join(x, 'index'), pkg, cb);
loadAsFile(path.join(x, 'index'), pkg, function (err, m, pkg) {
if (err) return cb(err);
if (m) return cb(null, m, pkg);
var incorrectMainError = new Error("Cannot find module '" + path.resolve(x, pkg.main) + "'. Please verify that the package.json has a valid \"main\" entry");
incorrectMainError.code = 'INCORRECT_PACKAGE_MAIN';
return cb(incorrectMainError);
});
});
});
return;
Expand Down
12 changes: 9 additions & 3 deletions lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,17 @@ module.exports = function resolveSync(x, options) {
pkg.main = 'index';
}
try {
var m = loadAsFileSync(path.resolve(x, pkg.main));
var mainPath = path.resolve(x, pkg.main);
var m = loadAsFileSync(mainPath);
if (m) return m;
var n = loadAsDirectorySync(path.resolve(x, pkg.main));
var n = loadAsDirectorySync(mainPath);
if (n) return n;
} catch (e) {}
var checkIndex = loadAsFileSync(path.resolve(x, 'index'));
if (checkIndex) return checkIndex;
} catch (e) { }
var incorrectMainError = new Error("Cannot find module '" + path.resolve(x, pkg.main) + "'. Please verify that the package.json has a valid \"main\" entry");
incorrectMainError.code = 'INCORRECT_PACKAGE_MAIN';
throw incorrectMainError;
}
}

Expand Down
46 changes: 46 additions & 0 deletions test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,18 @@ test('path iterator', function (t) {
});
});

test('empty main', function (t) {
t.plan(1);

var resolverDir = path.join(__dirname, 'resolver');
var dir = path.join(resolverDir, 'empty_main');

resolve('./empty_main', { basedir: resolverDir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'index.js'));
});
});

test('incorrect main', function (t) {
t.plan(1);

Expand All @@ -284,6 +296,40 @@ test('incorrect main', function (t) {
});
});

test('missing index', function (t) {
t.plan(2);

var resolverDir = path.join(__dirname, 'resolver');
resolve('./missing_index', { basedir: resolverDir }, function (err, res, pkg) {
t.ok(err instanceof Error);
t.equal(err && err.code, 'INCORRECT_PACKAGE_MAIN', 'error has correct error code');
});
});

test('missing main', function (t) {
t.plan(1);

var resolverDir = path.join(__dirname, 'resolver');
var dir = path.join(resolverDir, 'missing_main');

resolve('./missing_main', { basedir: resolverDir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'index.js'));
});
});

test('null main', function (t) {
t.plan(1);

var resolverDir = path.join(__dirname, 'resolver');
var dir = path.join(resolverDir, 'null_main');

resolve('./null_main', { basedir: resolverDir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'index.js'));
});
});

test('without basedir', function (t) {
t.plan(1);

Expand Down
Empty file.
3 changes: 3 additions & 0 deletions test/resolver/empty_main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": ""
}
3 changes: 3 additions & 0 deletions test/resolver/missing_index/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": "index.js"
}
Empty file.
3 changes: 3 additions & 0 deletions test/resolver/missing_main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"notmain": "index.js"
}
Empty file.
3 changes: 3 additions & 0 deletions test/resolver/null_main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": null
}

0 comments on commit 4bece07

Please sign in to comment.