Skip to content

Commit dfb5cbd

Browse files
committed
When json module is not found, include enabling --resolveJsonModule might help.
Fixes #25722
1 parent 7473772 commit dfb5cbd

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,6 +2220,12 @@ namespace ts {
22202220
const diag = Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead;
22212221
error(errorNode, diag, tsExtension, removeExtension(moduleReference, tsExtension));
22222222
}
2223+
else if (!compilerOptions.resolveJsonModule &&
2224+
fileExtensionIs(moduleReference, Extension.Json) &&
2225+
getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.NodeJs &&
2226+
getEmitModuleKind(compilerOptions) === ModuleKind.CommonJS) {
2227+
error(errorNode, Diagnostics.Cannot_find_module_0_Consider_using_resolveJsonModule_to_import_module_with_json_extension, moduleReference);
2228+
}
22232229
else {
22242230
error(errorNode, moduleNotFoundError, moduleReference);
22252231
}

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,10 @@
24212421
"category": "Error",
24222422
"code": 2730
24232423
},
2424+
"Cannot find module '{0}'. Consider using '--resolveJsonModule' to import module with '.json' extension": {
2425+
"category": "Error",
2426+
"code": 2731
2427+
},
24242428

24252429
"Import declaration '{0}' is using private name '{1}'.": {
24262430
"category": "Error",

tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModule.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
tests/cases/compiler/file1.ts(1,21): error TS2307: Cannot find module './b.json'.
2-
tests/cases/compiler/file1.ts(3,21): error TS2307: Cannot find module './b.json'.
1+
tests/cases/compiler/file1.ts(1,21): error TS2731: Cannot find module './b.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
2+
tests/cases/compiler/file1.ts(3,21): error TS2731: Cannot find module './b.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
33

44

55
==== tests/cases/compiler/file1.ts (2 errors) ====
66
import b1 = require('./b.json'); // error
77
~~~~~~~~~~
8-
!!! error TS2307: Cannot find module './b.json'.
8+
!!! error TS2731: Cannot find module './b.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
99
let x = b1.a;
1010
import b2 = require('./b.json'); // error
1111
~~~~~~~~~~
12-
!!! error TS2307: Cannot find module './b.json'.
12+
!!! error TS2731: Cannot find module './b.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
1313
if (x) {
1414
let b = b2.b;
1515
x = (b1.b === b);

0 commit comments

Comments
 (0)