/commands/typescript/tsc-compile-single-file
tsc file.ts: compile a single file (and why tsconfig.json is ignored)
- tool
- TypeScript
- versions
- TypeScript 6.0+ (TS5112, --ignoreConfig), TypeScript 7.0
- os
- any
npx tsc file.ts compiles just that file (and the files it imports) to file.js with the default compiler options: any tsconfig.json in the folder is not used. Since TypeScript 6.0 tsc refuses the mix and stops with error TS5112; add --ignoreConfig to compile the single file anyway, or drop the file name and run npx tsc to build the project with its config.
The TS5112 error and both fixes
npm init -y >/dev/null
npm install --silent --no-audit --no-fund --save-dev typescript@7.0.2 >/dev/null 2>&1
npx tsc --init >/dev/null
echo 'const greet = (name: string): string => `hello ${name}`; console.log(greet("ts"));' > hello.ts
npx tsc hello.ts
echo "--- with --ignoreConfig"
npx tsc --ignoreConfig hello.ts && node hello.js
echo "--- a type error"
echo 'const n: number = "x";' > bad.ts
npx tsc --ignoreConfig bad.tsOutput · exit 2
error TS5112: tsconfig.json is present but will not be loaded if files are specified on commandline. Use '--ignoreConfig' to skip this error. --- with --ignoreConfig hello ts --- a type error bad.ts(1,7): error TS2322: Type 'string' is not assignable to type 'number'.
With --ignoreConfig the file compiles with tsc's built-in defaults, and node hello.js runs the result. A type error is still reported, and the .js file is written anyway unless you add --noEmitOnError: that is what exit status 2 means (errors found, output generated), while TS5112 exits with 1.
Ways to compile
| Command | Uses tsconfig.json | Notes |
|---|---|---|
| npx tsc | Yes (current folder or a parent) | Builds every file that include/files select |
| npx tsc -p tsconfig.build.json | Yes, the named file | Also accepts a folder that contains a tsconfig.json |
| npx tsc hello.ts | No | TypeScript 6.0+: error TS5112 when a tsconfig.json exists; older versions silently use defaults |
| npx tsc --ignoreConfig hello.ts | No | Compile only this file and its imports with defaults plus flags given on the command line |
| npx tsc --ignoreConfig --target es2022 --outDir out hello.ts | No | Pass each option you need as a flag |
| npx tsc --watch | Yes | Recompile on every change |
Pitfalls
- Settings that seem to be ignored. On TypeScript 5.x and older,
tsc file.tssilently dropsstrict,target,outDir,pathsand everything else intsconfig.json. If a single-file build behaves differently from the project build, that is the reason. - Default options are old-fashioned. Without a config,
strictis off and the target and module defaults differ from thetsc --inittemplate, so code that passes this way can fail in the real build. Check withnpx tsc --noEmiton the project. - Output next to the source. Without
--outDir,hello.jslands besidehello.ts. - Imports are followed. Compiling one file also compiles every local file it imports, so "single file" can still emit several
.jsfiles.
Confirm it worked
After npx tsc --ignoreConfig file.ts, a file.js appears and echo $? is 0 when there were no errors. For a project build, npx tsc --showConfig shows which config and options tsc used.
FAQ
How do I compile one file but keep the tsconfig.json settings?
tsc has no option for that. Create a small config that extends the main one and lists the file in files, then run npx tsc -p that-config.json.
What does error TS5112 mean?
tsconfig.json is present but will not be loaded if files are specified on commandline. TypeScript 6.0 and later stop instead of silently ignoring the config. Remove the file names or add --ignoreConfig.
Can I run a .ts file without compiling it first?
Yes, with a runtime that strips types: Node.js 22.18+ and 23.6+ run .ts files directly with type stripping, and Bun and Deno run TypeScript natively. They do not type check, so keep tsc --noEmit for that.