Skip to content

Instantly share code, notes, and snippets.

@alanwei43
Created January 8, 2020 14:28
Show Gist options
  • Save alanwei43/e8c99bb8db51affb7064c0ea26b6ac9e to your computer and use it in GitHub Desktop.
Save alanwei43/e8c99bb8db51affb7064c0ea26b6ac9e to your computer and use it in GitHub Desktop.
import json by typescript

How to import *.json ?

As already answered you need Typescript >= 2.9 and the following settings in tsconfig.json:

{
  "resolveJsonModule": true,
  "esModuleInterop": true,
  "module": "commonjs"
}

But there are restrictions:

You must compile to CommonJS

All your imported JSONs must reside under the rootDir

Unfortunately the "rootDir" is very often a folder beneath package.json like './src' and things would fail.

So: How to import package.json ? You can require it: const pjson = require('../package.json');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment