Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

export JSON file from index file in typescript

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"]
    },
}
like image 852
Shubham Shaw Avatar asked Mar 08 '26 18:03

Shubham Shaw


1 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);
like image 128
ziishaned Avatar answered Mar 10 '26 06:03

ziishaned