'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?



Solution 1:[1]

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 it's config is running on your computer inside Node, which is totally different environment (compared with browser) with different API's and constraints

So there are two different configs for those two environments and two distinct sets of source files...

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 it's purpose more clear

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michal Levý