how to read json file properly with TypeScript?

Getting Error

error TS2732: Cannot find module '../service- .json'. Consider using '--resolveJsonModule' to import module with '.json sion.

import serviceAccountPlay from '../service-account.json';

I have this module file name -> typing.d.ts

   declare module "*.json" {
    const value: any;
    export default value;
      }

tsconfig.json

   tsconfig.json
   {
      "compilerOptions": {
     "module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": false,
"outDir": "lib",
"sourceMap": true,
"strict": false,
"resolveJsonModule": true,
"esModuleInterop": true,


 "target": "es2017"
  },
 "compileOnSave": true,
 "include": [
  "src"
  ]
  }

1 answer

  • answered 2021-06-20 02:59 basarat

    If you see this error error TS2732: Cannot find module '../service-.json'. Consider using '--resolveJsonModule' to import module with '.json. and modify your tsconfig.json the changes might not get reloaded.

    Quick Fix: Restart VSCode (or whatever IDE you are using). You do not need to modify any other options 🌹