rodrigocfd
rodrigocfd

Reputation: 8052

Why does Vite create two TypeScript config files: tsconfig.json and tsconfig.node.json?

I'm using Vite to create a new React + TypeScript project.

After creating the project, there are two TypeScript config files on the root folder: tsconfig.json and tsconfig.node.json. These are the contents of each one:

tsconfig.json

{
    "compilerOptions": {
        "target": "ESNext",
        "useDefineForClassFields": true,
        "lib": ["DOM", "DOM.Iterable", "ESNext"],
        "allowJs": false,
        "skipLibCheck": false,
        "esModuleInterop": false,
        "allowSyntheticDefaultImports": true,
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "module": "ESNext",
        "moduleResolution": "Node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "noEmit": true,
        "jsx": "react-jsx"
    },
    "include": ["src"],
    "references": [{ "path": "./tsconfig.node.json" }]
}

tsconfig.node.json

{
    "compilerOptions": {
        "composite": true,
        "module": "esnext",
        "moduleResolution": "node"
    },
    "include": ["vite.config.ts"]
}

Why do we need two?

What does the second one do?

Can I remove the second one?

Upvotes: 142

Views: 66607

Answers (3)

zatloeri
zatloeri

Reputation: 79

Adding on top of the already provided answers:

I did some digging and even checked the source code. The vite.config.ts seems to be transpiled and loaded directly using typescript API on each build/dev server restart so the reference to it from the "Project tsconfig" did not make much sense to me and then I found the following:

  1. Apparently it was kind of a hack to not include specific types, that Vite is dependent on, to the actual Project you are building (for example @types/node to a browser based environment) - Vite GitHub issue: tsconfig.node.json should not be included in tsconfig.json references
    • Issue is, that this does not seem to be working any more
    • This introduces other issues when using project references or other libraries relying on specific typescript setup
  2. That said, it might be used for different use cases in the future as seen in Vite GitHub pull request: use "solution" tsconfig so that vite.config.ts is type checked
    • One possibility is for compile time type checking as seen in the above PR

Upvotes: 6

Michal Levý
Michal Levý

Reputation: 37803

You need two different TS configs because the project is using two different environments in which the TypeScript code is executed:

  1. Your app (src folder) is targeting (will be running) inside the browser
  2. Vite itself including its config is running on your computer inside Node, which is totally different environment (compared with browser) with different API's and constraints

Thus there are two separate configs for those environments and two sets of source files that are defined by the include and exclude sections of these configs. Then there is one "master" config tsconfig.json which "rules them all"

And no, you probably don't want to delete the tsconfig.node.json but you can probably rename it to something like tsconfig.vite.json to make its purpose more clear. If you do, make sure to update "main" config accordingly

Upvotes: 115

timbhison
timbhison

Reputation: 171

As mentioned by Michal Levý, these are different configs for different environments.

You will notice that tsconfig.json includes a "references" key which points to an array that includes the tsconfig.node.json file. If you wish to change the filename of your vite tsconfig, be sure to update this reference.

Upvotes: 12

Related Questions