最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - export JSON file from index file in typescript - Stack Overflow

programmeradmin5浏览0评论

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"]
    },
}

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"]
    },
}
Share Improve this question asked Sep 2, 2021 at 11:15 Shubham ShawShubham Shaw 1,0053 gold badges15 silver badges30 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 22

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';
发布评论

评论列表(0)

  1. 暂无评论