Typescript: Option resolveJsonModule doesn't work without esModuleInterop

Created on 3 Jul 2018  路  22Comments  路  Source: microsoft/TypeScript


TypeScript Version: 2.9.2, 3.0.0-dev.20180703


Search Terms:

resolveJsonModule, esModuleInterop

Code

import * as test from './test.json';

tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "moduleResolution": "node",
        "target": "es2017",
        "lib": ["es2017"],
        "strict": true,
        "sourceMap": true,
        "noEmitOnError": true,
        "baseUrl": ".",
        "resolveJsonModule": true,
        "outDir": "out"
    }
}

Expected behavior:

No errors.

Actual behavior:

Compile error:

$ tsc
test.ts:1:23 - error TS2497: Module '"/home/kostya/tmp/resolve-json-test/test"' resolves to a non-module entity and cannot be imported using this construct.

1 import * as test from './test.json';
                        ~~~~~~~~~~~~~

When I add esModuleInterop option to tsconfig.json:

diff --git a/tsconfig.json b/tsconfig.json
index 7f1afb8..e949135 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -9,6 +9,7 @@
         "noEmitOnError": true,
         "baseUrl": ".",
         "resolveJsonModule": true,
+        "esModuleInterop": true,
         "outDir": "out"
     }
 }

and change import statement to:

diff --git a/test.ts b/test.ts
index 07bb9b7..dddcffb 100644
--- a/test.ts
+++ b/test.ts
@@ -1 +1 @@
-import * as test from './test.json';
+import test from './test.json';

it works. But with esModuleInterop option I need to change many imports in my project, which is undesirable.

Playground Link:

Related Issues:

Bug Fixed

Most helpful comment

After setting resolveJsonModule to "true", I was having the same issue in VS Code, (TypeScript 3.2.2, VSCode 1.30.1). Restarting VS Code resolved the issue for me - I did not have to alter any other ts config

All 22 comments

Also with resolveJsonModule, but without esModuleInterop, json file appears in output directory.

After setting resolveJsonModule to "true", I was having the same issue in VS Code, (TypeScript 3.2.2, VSCode 1.30.1). Restarting VS Code resolved the issue for me - I did not have to alter any other ts config

Confirmed that setting resolveJsonModule to true in my tsconfig.json resolved this issue.

Manually restarting VS Code resolves this issue.

After setting resolveJsonModule to "true", I was having the same issue in VS Code, (TypeScript 3.2.2, VSCode 1.30.1). Restarting VS Code resolved the issue for me - I did not have to alter any other ts config

Thank you, it worked!

After setting resolveJsonModule to "true", I was having the same issue in VS Code, (TypeScript 3.2.2, VSCode 1.30.1). Restarting VS Code resolved the issue for me - I did not have to alter any other ts config

Worked for me as well, thanks!

After setting resolveJsonModule to "true", I was having the same issue in VS Code, (TypeScript 3.2.2, VSCode 1.30.1). Restarting VS Code resolved the issue for me - I did not have to alter any other ts config

This actually worked. Thanks 馃槂

After setting resolveJsonModule to "true", I was having the same issue in VS Code, (TypeScript 3.2.2, VSCode 1.30.1). Restarting VS Code resolved the issue for me - I did not have to alter any other ts config

Thanks, It worked like a charm!

I see similar behavior in intellij to what @intelliot described above for vs code. Thanks for the suggestion.

For those who have updated their target to esnext you need to add the following to your tsconfig:
...
"target" : "esnext",
"moduleResolution": "node",
...

Initially after a restart the file is green, then 3 seconds later highlighted in red again. So restarting in my case doesn't fix this issue.

@chrisj-skinner same with me. Not sure why this is happening.

Why is this issue close since this problem still exists ?

I have

    "esModuleInterop": true,
    "resolveJsonModule": true,
    "moduleResolution": "node",

and
const pkgJSON = require('../../package.json');

- Consider using '--resolveJsonModule' to import module with '.json' extensionts(2732)

I use resolveJsonModule already.

If you use WebStorm you shall also restart.

I have the same issue here,

Initially after a restart the file is green, then 3 seconds later highlighted in red again. So restarting in my case doesn't fix this issue.

Initially after a restart the file is green, then 3 seconds later highlighted in red again. So restarting in my case doesn't fix this issue.

I had same issue, but that because I forgot to include the new created folder / ts file in the tsconfig.json

I had same issue, but that because I forgot to include the new created folder / ts file in the tsconfig.json

Same, thanks bro!

In my case: "File" -> "Invalidate Caches / Restart..." -> "Invalidate and Restart", solved the issue

In my case, I fixed this by doing:

        "moduleResolution": "node"

This can also fix the issue:

        "module": "commonjs"

For my case, I'm unsure if this is suppose to happen or not but, I had to put my tsconfig.json in the same directory as my out and src folders.

Although I added moduleResolution: node, the error was still there. After a VS restart it got 'fixed'

Was this page helpful?
0 / 5 - 0 ratings