Reputation: 51
I am trying to import a JSON file into a typescript program, and keep receiving error TS2732: Cant find module. the file with the JSON file i am trying to import is in the src folder along with the main.ts file. my code is as follows
import logs = require('./logs/logs_0.json')
console.log(logs.id)
Upvotes: 5
Views: 2818
Reputation: 2463
Not sure what version of typescript you're running but there was a bug introduced in version 2.9.0 that was fixed with 2.9.2
In your tsconfig.json, under the compiler options make sure to have:
"resolveJsonModule": true,
"esModuleInterop": true,
The esModuleInterop is only necessary for the default import of the json file. If you leave it set to false then you have to import it with import * as logs0 from '../logs_0.json'
Upvotes: 4
Reputation: 61
If you have resolveJsonModule
set to true in tsconfig.json as @Daniel M suggested, you can try something like this:
import logs from './logs/logs_0.json';
console.log(logs.id);
require
is something you would usually use in a javascript file.
Also make sure that on the same level with the file that has this import, you have logs
folder, which contains your json. If json file is on the same level as typescript file, you might want to try importing from ./logs_0.json
.
Upvotes: 1