Is it possible to include a json file when using TypeScript with a tsconfig.json file?

JsonTypescript

Json Problem Overview


Is there any way to have the TypeScript compiler also copy content files that don't have a ts or tsx extension to the output directory?

For example, my tsconfig.json looks like:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noImplicitAny": false,
        "sourceMap": true,
        "outDir": "../../dist",
        "types": [
        ]
    },
    "include": [
        "file1.ts",
        "config.json"
    ],
    "exclude": [
        "../../node_modules"
    ]
}

I would like the config.json to end up in my ../../dist directory, but this isn't happening. Is there no concept of a content file for TypeScript?

Json Solutions


Solution 1 - Json

This can be achieved by setting "resolveJsonModule": true in the compiler options and adding "source-path/**/*.json" to the include array in the tsconfig.json

Solution 2 - Json

I was having the same problem and the solution for this was to add:

"source-path/**/*.json"

keeping into "includes" array:

"source-path/**/*"

Example:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "resolveJsonModule": true,
    "incremental": true
  },
  "include":[ 
    "src/**/*",
    "src/**/*.json",
  ],
}

Solution 3 - Json

One workaround is to make the json file a normal js file, require/import it normally, and add "allowJs" : true to "compilerOptions" in tsconfig.json.

Solution 4 - Json

No, the tsc command's job is only to compile .ts files. It doesn't have the capabilities to do build-related tasks that a build system like gulp, grunt or webpack would handle.

For more information, see this similar question: https://stackoverflow.com/questions/38708200/angular-2-typescript-compiler-copy-html-and-css-files

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrian VallelungaView Question on Stackoverflow
Solution 1 - JsonArun ReddyView Answer on Stackoverflow
Solution 2 - JsonRodrigo AlcortaView Answer on Stackoverflow
Solution 3 - JsonAlex SharpView Answer on Stackoverflow
Solution 4 - JsonvogtbView Answer on Stackoverflow