Skip to content

Commit 2b14bcb

Browse files
authored
Merge pull request microsoft#25949 from Microsoft/resolveJsonModuleError
When json module is not found, include enabling --resolveJsonModule might help.
2 parents f12e9a8 + d920efe commit 2b14bcb

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
@@ -2425,6 +2425,10 @@
24252425
"category": "Error",
24262426
"code": 2731
24272427
},
2428+
"Cannot find module '{0}'. Consider using '--resolveJsonModule' to import module with '.json' extension": {
2429+
"category": "Error",
2430+
"code": 2732
2431+
},
24282432

24292433
"Import declaration '{0}' is using private name '{1}'.": {
24302434
"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 TS2732: Cannot find module './b.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
2+
tests/cases/compiler/file1.ts(3,21): error TS2732: 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 TS2732: 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 TS2732: 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)