Skip to content

Commit

Permalink
test: use index.js if package.json "main" is empty
Browse files Browse the repository at this point in the history
Verify that the module loader uses index.js when the "main" property of
package.json is the empty string.

Refs: #32013

PR-URL: #32040
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Denys Otrishko <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
bnoordhuis authored and MylesBorins committed Mar 9, 2020
1 parent 3d64c9e commit 862cd2b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/fixtures/require-empty-main/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
module.exports = 42;
1 change: 1 addition & 0 deletions test/fixtures/require-empty-main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"main":""}
25 changes: 25 additions & 0 deletions test/parallel/test-require-empty-main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';
require('../common');

// A package.json with an empty "main" property should use index.js if present.
// require.resolve() should resolve to index.js for the same reason.
//
// In fact, any "main" property that doesn't resolve to a file should result
// in index.js being used, but that's already checked for by other tests.
// This test only concerns itself with the empty string.

const assert = require('assert');
const path = require('path');
const fixtures = require('../common/fixtures');

const where = fixtures.path('require-empty-main');
const expected = path.join(where, 'index.js');

test();
setImmediate(test);

function test() {
assert.strictEqual(require.resolve(where), expected);
assert.strictEqual(require(where), 42);
assert.strictEqual(require.resolve(where), expected);
}

0 comments on commit 862cd2b

Please sign in to comment.