Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/internal/main/check_syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ async function checkSyntax(source, filename) {
format = await defaultGetFormat(new URL(url));
}

// A `.js` file with no `"type"` in the nearest package.json has no format of
// its own. At load time the goal is decided by looking for module syntax in
// the source, so decide it the same way here. Otherwise such a file is only
// ever parsed as CommonJS, where module syntax makes the parse bail out
// before any syntax error in the rest of the file is reported.
if (format === null || format === undefined) {
const { containsModuleSyntax } = internalBinding('contextify');
if (containsModuleSyntax(source, filename)) {
format = 'module';
}
}

if (format === 'module') {
const { ModuleWrap } = internalBinding('module_wrap');
new ModuleWrap(filename, undefined, source, 0, 0);
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/syntax/bad_syntax_esm_ambiguous.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import fs from 'node:fs';
var = ;
3 changes: 3 additions & 0 deletions test/sequential/test-cli-syntax-bad.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const syntaxErrorRE = /^SyntaxError: \b/m;
'syntax/bad_syntax',
'syntax/bad_syntax_shebang.js',
'syntax/bad_syntax_shebang',
// A `.js` file with no `"type"` in the nearest package.json, whose module
// syntax makes it load as ESM. Refs: https://github.com/nodejs/node/issues/65202
'syntax/bad_syntax_esm_ambiguous.js',
].forEach((file) => {
const path = fixtures.path(file);

Expand Down
Loading