export JSON file from index file in typescript

Viewed 1898

in my typescript project, i am able to re-export my default exports in index.ts of a folder.

for eg:

export { default as BaseError } from "./errorResponse";

but i am unable to export JSON files like this.

export * as configFiles from "./config";

how to bundle and export JSON files such that i can use it in paths of tsconfig

{
  "compilerOptions": {
    "paths": {
      "@errorClass": ["src/helpers/errorClasses"],
      "@config": ["src/config"],
      "@routes": ["src/routes"]
    },
}
2 Answers

Add the following options inside your tsconfig.json file:

{
  "compilerOptions": {
    "esModuleInterop": true,
    "resolveJsonModule": true
  }
}

After updating tsconfig file export configFile like below:

export { default as configFiles } from "./config.json";

Now to import the configFiles inside your code use below:

import { configFiles } from "./file-path-to-configFiles-variable";

console.log(configFiles);

example tsconfig.json

"compilerOptions": {
        "baseUrl": "src",
        ...
        "paths": {
            "@config":["src/config/*"],
         ...
        },`
import { xConfig } from '@config/index';
Related