Skip to content

Commit 4bece07

Browse files
pingrishabhljharb
authored andcommitted
[Fix] throw an error for an invalid explicit main with a missing ./index.js file
Fixes #223. Fixes #143.
1 parent 561e223 commit 4bece07

File tree

10 files changed

+74
-4
lines changed

10 files changed

+74
-4
lines changed

lib/async.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,13 @@ module.exports = function resolve(x, options, callback) {
267267
loadAsDirectory(dir, pkg, function (err, n, pkg) {
268268
if (err) return cb(err);
269269
if (n) return cb(null, n, pkg);
270-
loadAsFile(path.join(x, 'index'), pkg, cb);
270+
loadAsFile(path.join(x, 'index'), pkg, function (err, m, pkg) {
271+
if (err) return cb(err);
272+
if (m) return cb(null, m, pkg);
273+
var incorrectMainError = new Error("Cannot find module '" + path.resolve(x, pkg.main) + "'. Please verify that the package.json has a valid \"main\" entry");
274+
incorrectMainError.code = 'INCORRECT_PACKAGE_MAIN';
275+
return cb(incorrectMainError);
276+
});
271277
});
272278
});
273279
return;

lib/sync.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,17 @@ module.exports = function resolveSync(x, options) {
168168
pkg.main = 'index';
169169
}
170170
try {
171-
var m = loadAsFileSync(path.resolve(x, pkg.main));
171+
var mainPath = path.resolve(x, pkg.main);
172+
var m = loadAsFileSync(mainPath);
172173
if (m) return m;
173-
var n = loadAsDirectorySync(path.resolve(x, pkg.main));
174+
var n = loadAsDirectorySync(mainPath);
174175
if (n) return n;
175-
} catch (e) {}
176+
var checkIndex = loadAsFileSync(path.resolve(x, 'index'));
177+
if (checkIndex) return checkIndex;
178+
} catch (e) { }
179+
var incorrectMainError = new Error("Cannot find module '" + path.resolve(x, pkg.main) + "'. Please verify that the package.json has a valid \"main\" entry");
180+
incorrectMainError.code = 'INCORRECT_PACKAGE_MAIN';
181+
throw incorrectMainError;
176182
}
177183
}
178184

test/resolver.js

+46
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,18 @@ test('path iterator', function (t) {
272272
});
273273
});
274274

275+
test('empty main', function (t) {
276+
t.plan(1);
277+
278+
var resolverDir = path.join(__dirname, 'resolver');
279+
var dir = path.join(resolverDir, 'empty_main');
280+
281+
resolve('./empty_main', { basedir: resolverDir }, function (err, res, pkg) {
282+
if (err) t.fail(err);
283+
t.equal(res, path.join(dir, 'index.js'));
284+
});
285+
});
286+
275287
test('incorrect main', function (t) {
276288
t.plan(1);
277289

@@ -284,6 +296,40 @@ test('incorrect main', function (t) {
284296
});
285297
});
286298

299+
test('missing index', function (t) {
300+
t.plan(2);
301+
302+
var resolverDir = path.join(__dirname, 'resolver');
303+
resolve('./missing_index', { basedir: resolverDir }, function (err, res, pkg) {
304+
t.ok(err instanceof Error);
305+
t.equal(err && err.code, 'INCORRECT_PACKAGE_MAIN', 'error has correct error code');
306+
});
307+
});
308+
309+
test('missing main', function (t) {
310+
t.plan(1);
311+
312+
var resolverDir = path.join(__dirname, 'resolver');
313+
var dir = path.join(resolverDir, 'missing_main');
314+
315+
resolve('./missing_main', { basedir: resolverDir }, function (err, res, pkg) {
316+
if (err) t.fail(err);
317+
t.equal(res, path.join(dir, 'index.js'));
318+
});
319+
});
320+
321+
test('null main', function (t) {
322+
t.plan(1);
323+
324+
var resolverDir = path.join(__dirname, 'resolver');
325+
var dir = path.join(resolverDir, 'null_main');
326+
327+
resolve('./null_main', { basedir: resolverDir }, function (err, res, pkg) {
328+
if (err) t.fail(err);
329+
t.equal(res, path.join(dir, 'index.js'));
330+
});
331+
});
332+
287333
test('without basedir', function (t) {
288334
t.plan(1);
289335

test/resolver/empty_main/index.js

Whitespace-only changes.

test/resolver/empty_main/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"main": ""
3+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"main": "index.js"
3+
}

test/resolver/missing_main/index.js

Whitespace-only changes.
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"notmain": "index.js"
3+
}

test/resolver/null_main/index.js

Whitespace-only changes.

test/resolver/null_main/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"main": null
3+
}

0 commit comments

Comments
 (0)