question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot compile with TS 2.9.1 & "resolveJsonModule"

See original GitHub issue

Expected Behaviour

TS 2.9 added a new option to allow JSON imports and get proper types doing so (without resorting to somewhat dirty workarounds using ambient definitions), with the resolveJsonModule compiler option.

It should compile as usual when using this new flag.

Actual Behaviour

An error is thrown when running webpack with ts-loader:

/Users/mehdibenadda/Development/fun-cms/node_modules/typescript/lib/typescript.js:107386
            return program.getOptionsDiagnostics(cancellationToken).concat(program.getGlobalDiagnostics(cancellationToken));
                           ^

TypeError: Cannot read property 'getOptionsDiagnostics' of undefined
    at Object.getCompilerOptionsDiagnostics (/Users/mehdibenadda/Development/fun-cms/node_modules/typescript/lib/typescript.js:107386:28)
    at provideCompilerOptionDiagnosticErrorsToWebpack (/Users/mehdibenadda/Development/fun-cms/node_modules/ts-loader/dist/after-compile.js:39:31)
    at /Users/mehdibenadda/Development/fun-cms/node_modules/ts-loader/dist/after-compile.js:17:9
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/mehdibenadda/Development/fun-cms/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:16:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/mehdibenadda/Development/fun-cms/node_modules/tapable/lib/Hook.js:35:21)
    at compilation.seal.err (/Users/mehdibenadda/Development/fun-cms/node_modules/webpack/lib/Compiler.js:497:30)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/mehdibenadda/Development/fun-cms/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/mehdibenadda/Development/fun-cms/node_modules/tapable/lib/Hook.js:35:21)
    at hooks.optimizeAssets.callAsync.err (/Users/mehdibenadda/Development/fun-cms/node_modules/webpack/lib/Compilation.js:985:35)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/mehdibenadda/Development/fun-cms/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/mehdibenadda/Development/fun-cms/node_modules/tapable/lib/Hook.js:35:21)
    at hooks.optimizeChunkAssets.callAsync.err (/Users/mehdibenadda/Development/fun-cms/node_modules/webpack/lib/Compilation.js:976:32)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/mehdibenadda/Development/fun-cms/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/mehdibenadda/Development/fun-cms/node_modules/tapable/lib/Hook.js:35:21)
    at hooks.additionalAssets.callAsync.err (/Users/mehdibenadda/Development/fun-cms/node_modules/webpack/lib/Compilation.js:971:36)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/mehdibenadda/Development/fun-cms/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/mehdibenadda/Development/fun-cms/node_modules/tapable/lib/Hook.js:35:21)
    at hooks.optimizeTree.callAsync.err (/Users/mehdibenadda/Development/fun-cms/node_modules/webpack/lib/Compilation.js:967:32)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/mehdibenadda/Development/fun-cms/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/mehdibenadda/Development/fun-cms/node_modules/tapable/lib/Hook.js:35:21)
    at Compilation.seal (/Users/mehdibenadda/Development/fun-cms/node_modules/webpack/lib/Compilation.js:904:27)
    at hooks.make.callAsync.err (/Users/mehdibenadda/Development/fun-cms/node_modules/webpack/lib/Compiler.js:494:17)
    at _err0 (eval at create (/Users/mehdibenadda/Development/fun-cms/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:11:1)
    at _addModuleChain (/Users/mehdibenadda/Development/fun-cms/node_modules/webpack/lib/Compilation.js:770:12)
    at processModuleDependencies.err (/Users/mehdibenadda/Development/fun-cms/node_modules/webpack/lib/Compilation.js:709:9)
    at process._tickCallback (internal/process/next_tick.js:150:11)

Steps to Reproduce the Problem

Compile a project using webpack, ts-loader and typescript@2.9.1 with resolveJsonModule (and esModuleInterop) enabled and an import for a JSON file somewhere in the project.

Removing resolveJsonModule and using an ambient declaration works fine. I tried this with ts-loader@4.4.1 and typescript@next

Location of a Minimal Repository that Demonstrates the Issue.

https://github.com/mbenadda/ts-loader-issue-793-example-repo

See instructions in README, just clone, yarn install and yarn webpack to see the issue.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:16 (10 by maintainers)

github_iconTop GitHub Comments

4reactions
noahmcommented, Jul 30, 2018

This issue should now be resolved with today’s release of Typescript 3.0 😄

3reactions
johnnyreillycommented, Jun 14, 2018

When this TypeScript PR gets shipped the issue should be resolved: https://github.com/Microsoft/TypeScript/pull/24959

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to read json file properly with TypeScript? - Stack Overflow
If you see this error error TS2732: Cannot find module '../service-.json'. Consider using '--resolveJsonModule' to import module with ...
Read more >
Importing JSON Modules in TypeScript - Marius Schulz
TypeScript 2.9 introduced a new --resolveJsonModule compiler option that lets us import JSON modules from within TypeScript modules.
Read more >
Angular , Ionic - ZimBaroo's Geeks Sharing
Not able to import json file in the TypeScript in Angular- Cannot find module ... Consider using '--resolveJsonModule' to import module with ...
Read more >
TSConfig Reference - Docs on every TSConfig option
An error occurs if any of the files can't be found. ... Consider using '--resolveJsonModule' to import module with '.json' extension.
Read more >
How To Read Local JSON Files In Angular
json file under compiler options we need to add “resolveJsonModule” and ”esModuleInterop” configurations as true as shown below. { " ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found