module: check --experimental-require-module separately from detection · nodejs/node@1db210a · GitHub
Skip to content

Commit 1db210a

Browse files
joyeecheungmarco-ippolito
authored andcommitted
module: check --experimental-require-module separately from detection
Previously we assumed if `--experimental-detect-module` is true, then `--experimental-require-module` is true, which isn't the case, as the two can be enabled/disabled separately. This patch fixes the checks so `--no-experimental-require-module` is still effective when `--experimental-detect-module` is enabled. Drive-by: make the assertion messages more informative and remove obsolete TODO about allowing TLA in entrypoints handled by require(esm). PR-URL: #55250 Backport-PR-URL: #56927 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Refs: #52697
1 parent ececd22 commit 1db210a

3 files changed

Lines changed: 34 additions & 5 deletions

File tree

lib/internal/modules/cjs/loader.js

Lines changed: 12 additions & 4 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Flags: --no-experimental-require-module
2+
'use strict';
3+
4+
// Tests that --experimental-require-module is not implied by --experimental-detect-module
5+
// and is checked independently.
6+
require('../common');
7+
const assert = require('assert');
8+
9+
// Check that require() still throws SyntaxError for an ambiguous module that's detected to be ESM.
10+
// TODO(joyeecheung): now that --experimental-detect-module is unflagged, it makes more sense
11+
// to either throw ERR_REQUIRE_ESM for require() of detected ESM instead, or add a hint about the
12+
// use of require(esm) to the SyntaxError.
13+
14+
assert.throws(
15+
() => require('../fixtures/es-modules/loose.js'),
16+
{
17+
name: 'SyntaxError',
18+
message: /Unexpected token 'export'/
19+
});

test/fixtures/es-modules/loose.js

Lines changed: 3 additions & 1 deletion

0 commit comments

Comments
 (0)