Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: importing js libraries that import json file

Some JS libraries (e.g. https://www.npmjs.com/package/winston) require json files in their code (the most common use case being package.json). Typescript will throw an error when importing such libraries, as it only understand .ts and .tsx.

Can we teach typescript how to handle importing json files like the way node handles it?

like image 890
Alireza Mirian Avatar asked May 12 '26 00:05

Alireza Mirian


1 Answers

TS 2.9 added support for json imports. Just add:

{
  "compilerOptions": {
    "resolveJsonModule": true
  }
}

in your tsconfig.json or jsconfig.json

like image 181
Matt Bierner Avatar answered May 14 '26 14:05

Matt Bierner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!