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 <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
bnoordhuis authored and codebytere committed Mar 30, 2020
1 parent 03aa2e1 commit 1470457
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 1470457

Please sign in to comment.