How to Fix "Cannot find module 'file.json'" TypeScript Error?

When trying to import a JSON file in your TypeScript project, you may see the following error:

Cannot find module 'my-json-file.json'. Consider using '--resolveJsonModule' to import module with '.json' extension

This happens because TypeScript does not support resolving JSON files by default. If you run into this issue, then you can simply add the resolveJsonModule directive under compilerOptions in your tsconfig.json file, for example, like so:

{
  "compilerOptions": {
    // ...
    "resolveJsonModule": true,
    // ...
  }
}

This will:

  1. Allow importing modules with a ".json" file extension, and;
  2. Generate a type for the imported JSON (based on the static JSON shape).

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.