"outDir" and "rootDir"

The folder where compiled code goes, and the folder of the sources.

On this page

Example

#
JSON
{
  "compilerOptions": {
    "rootDir": "./src",
    "outDir": "./dist",
    "declaration": true
  },
  "include": ["src"]
}

Definition and usage

#

The outDir option points to the folder for generated JavaScript files (and optionally .d.ts files). Without it, the compiler writes the output next to the source files, which quickly makes a mess in the project.

rootDir sets the base folder of the sources. The folder structure inside rootDir is reproduced in outDir: the file src/utils/format.ts ends up in dist/utils/format.js.

In projects with a bundler or framework (Next.js, Vite), the build tool does the compilation, and tsconfig often has noEmit: true. In that case outDir doesn’t matter.

Syntax

#
Syntax
"rootDir": "./src",
"outDir": "./dist"

Good practices

#
  • Add the dist folder to .gitignore. Generated code shouldn’t go into the repository.
  • The declaration: true option generates .d.ts files with types, which you need when publishing libraries.

Related entries

#

Spotted a mistake or missing an example? Write to us.