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

Find out why this TypeScript error happens and how to fix it

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).

Hope you found this post useful. It was published . Please show your love and support by sharing this post.