3

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?

1 Answer 1

1

TS 2.9 added support for json imports. Just add:

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

in your tsconfig.json or jsconfig.json

Sign up to request clarification or add additional context in comments.

3 Comments

This not solve the problem in my case. Using a library that has an import like require(path.resolve(process.cwd() + '/config.json')). To take the config file from the root path.
JavaScript works fine, however, using this library on TypeScript throws an error. It can be fixed in some way on the codebase of the lib?
Yes, IntelliSense generally cannot resolve dynamic imports like that (especially because that example depends on where the script is run from). In such cases, your best bet is to try explicitly typing the import using jsdoc

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.