Skip to content

Give more specific errors for verbatimModuleSyntax #62113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 23, 2025
Merged
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
22 changes: 18 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2528,6 +2528,20 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return diagnostic;
}

function getVerbatimModuleSyntaxErrorMessage(node: Node): DiagnosticMessage {
const sourceFile = getSourceFileOfNode(node);
const fileName = sourceFile.fileName;

// Check if the file is .cts or .cjs (CommonJS-specific extensions)
if (fileExtensionIsOneOf(fileName, [Extension.Cts, Extension.Cjs])) {
return Diagnostics.ECMAScript_imports_and_exports_cannot_be_written_in_a_CommonJS_file_under_verbatimModuleSyntax;
}
else {
// For .ts, .tsx, .js, etc.
return Diagnostics.ECMAScript_imports_and_exports_cannot_be_written_in_a_CommonJS_file_under_verbatimModuleSyntax_Adjust_the_type_field_in_the_nearest_package_json_to_make_this_file_an_ECMAScript_module_or_adjust_your_verbatimModuleSyntax_module_and_moduleResolution_settings_in_TypeScript;
}
}

function addErrorOrSuggestion(isError: boolean, diagnostic: Diagnostic) {
if (isError) {
diagnostics.add(diagnostic);
Expand Down Expand Up @@ -48325,7 +48339,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
!isInJSFile(node) &&
host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) === ModuleKind.CommonJS
) {
error(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled);
error(node, getVerbatimModuleSyntaxErrorMessage(node));
}
else if (
moduleKind === ModuleKind.Preserve &&
Expand All @@ -48337,7 +48351,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// when we look at the `impliedNodeFormat` of this file and decide it's CommonJS (i.e., currently,
// only if the file extension is .cjs/.cts). To avoid that inconsistency, we disallow ESM syntax
// in files that are unambiguously CommonJS in this mode.
error(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_module_is_set_to_preserve);
error(node, Diagnostics.ECMAScript_module_syntax_is_not_allowed_in_a_CommonJS_module_when_module_is_set_to_preserve);
}

if (
Expand Down Expand Up @@ -48769,7 +48783,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

if (isIllegalExportDefaultInCJS) {
error(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled);
error(node, getVerbatimModuleSyntaxErrorMessage(node));
}

checkExternalModuleExports(container);
Expand Down Expand Up @@ -53334,7 +53348,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

function checkGrammarImportCallExpression(node: ImportCall): boolean {
if (compilerOptions.verbatimModuleSyntax && moduleKind === ModuleKind.CommonJS) {
return grammarErrorOnNode(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled);
return grammarErrorOnNode(node, getVerbatimModuleSyntaxErrorMessage(node));
}

if (node.expression.kind === SyntaxKind.MetaProperty) {
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@
"category": "Error",
"code": 1285
},
"ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.": {
"ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'.": {
"category": "Error",
"code": 1286
},
Expand Down Expand Up @@ -967,14 +967,18 @@
"category": "Error",
"code": 1292
},
"ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'.": {
"ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'.": {
"category": "Error",
"code": 1293
},
"This syntax is not allowed when 'erasableSyntaxOnly' is enabled.": {
"category": "Error",
"code": 1294
},
"ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.": {
"category": "Error",
"code": 1295
},
"'with' statements are not allowed in an async function block.": {
"category": "Error",
"code": 1300
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
bad.ts(1,10): error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.
bad.ts(1,10): error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
bad.ts(1,16): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
bad.ts(1,16): error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.
bad.ts(1,16): error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
good.ts(2,10): error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.


==== ./types.ts (0 errors) ====
Expand Down Expand Up @@ -36,11 +36,11 @@ good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when
==== ./bad.ts (4 errors) ====
import { Date, Event } from './types';
~~~~
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
!!! error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.
~~~~
!!! error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
~~~~~
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
!!! error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.
~~~~~
!!! error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
function foo(a: Date) {
Expand All @@ -55,7 +55,7 @@ good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when
import type { Date, Event } from './types';
import { Console } from 'node:console';
~~~~~~~
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
!!! error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.
function foo(a: Date) {
const b = new Date(a.year, a.month, a.day);
return b.getTime();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
bad.ts(1,10): error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.
bad.ts(1,10): error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
bad.ts(1,10): error TS2866: Import 'Date' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
bad.ts(1,16): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
bad.ts(1,16): error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.
bad.ts(1,16): error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
bad.ts(1,16): error TS2866: Import 'Event' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
good.ts(2,10): error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.


==== ./types.ts (0 errors) ====
Expand Down Expand Up @@ -38,13 +38,13 @@ good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when
==== ./bad.ts (6 errors) ====
import { Date, Event } from './types';
~~~~
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
!!! error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.
~~~~
!!! error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
~~~~
!!! error TS2866: Import 'Date' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
~~~~~
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
!!! error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.
~~~~~
!!! error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
~~~~~
Expand All @@ -61,7 +61,7 @@ good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when
import type { Date, Event } from './types';
import { Console } from 'node:console';
~~~~~~~
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
!!! error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.
function foo(a: Date) {
const b = new Date(a.year, a.month, a.day);
return b.getTime();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
bad.ts(1,10): error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.
bad.ts(1,10): error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.


Expand All @@ -8,7 +8,7 @@ bad.ts(1,10): error TS1484: 'FC' is a type and must be imported using a type-onl
==== bad.ts (2 errors) ====
import { FC } from "./types";
~~
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
!!! error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.
~~
!!! error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
let FC: FC | null = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
bad.ts(1,10): error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.
bad.ts(1,10): error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
bad.ts(1,10): error TS2865: Import 'FC' conflicts with local value, so must be declared with a type-only import when 'isolatedModules' is enabled.

Expand All @@ -9,7 +9,7 @@ bad.ts(1,10): error TS2865: Import 'FC' conflicts with local value, so must be d
==== bad.ts (3 errors) ====
import { FC } from "./types";
~~
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
!!! error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.
~~
!!! error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
~~
Expand Down
Loading
Loading