How to fix Cannot use --outDir without specifying file names with .ts extensions in TypeScript
This TypeScript compilation error occurs when you use the --outDir flag without providing TypeScript source files (.ts or .tsx extensions). The TypeScript compiler needs source files to determine what to compile and where to output the JavaScript files.
Quick fix
Fixes Cannot use --outDir without specifying file names with .ts extensions
# Correct: Specify .ts files with --outDir
tsc --outDir dist src/*.ts
# Correct: Use tsconfig.json instead
tsc --outDir dist --project tsconfig.json
# Incorrect: Missing .ts files
tsc --outDir dist # This will cause the error- Check your tsc command syntax
- Use tsconfig.json for project compilation
- Verify file patterns match TypeScript files
- Check tsconfig.json include/exclude patterns
# Correct: Specify .ts files with --outDirtsc --outDir dist src/*.ts# Correct: Use tsconfig.json insteadtsc --outDir dist --project tsconfig.json# Incorrect: Missing .ts filestsc --outDir dist # This will cause the errorCannot use --outDir without specifying file names with .ts extensions
What this error means
When you run the TypeScript compiler (tsc) with the --outDir flag, you're telling it to output compiled JavaScript files to a specific directory. However, the compiler needs to know which TypeScript source files to compile. This error typically happens in two scenarios: 1. You're running tsc with --outDir but not specifying any .ts or .tsx files on the command line 2. You're using a tsconfig.json file that doesn't properly specify the files or include patterns The TypeScript compiler requires explicit source files when using --outDir because it needs to know what to compile and where to place the output relative to the source file structure. Without source files, it cannot determine the output directory structure.
Typical symptoms
- TypeScript compilation fails with error: "Cannot use --outDir without specifying file names with .ts extensions"
- Build scripts or CI/CD pipelines fail when running tsc commands
- You see the error when trying to compile TypeScript from the command line
- The error appears even when you have a tsconfig.json file present
Common causes
- Running tsc --outDir dist without specifying any .ts files on the command line
- Using tsc --outDir dist with only JavaScript (.js) files specified
- Incorrect tsconfig.json configuration that doesn't include any TypeScript source files
- Using --outDir flag with file patterns that don't match any .ts or .tsx files
How to fix "Cannot use --outDir without specifying file names with .ts extensions"
Verify you're specifying TypeScript source files when using --outDir:
# Correct: Specify .ts files with --outDir
tsc --outDir dist src/*.ts
# Correct: Use tsconfig.json instead
tsc --outDir dist --project tsconfig.json
# Incorrect: Missing .ts files
tsc --outDir dist # This will cause the errorAlways include .ts or .tsx files when using --outDir on the command line.
Instead of specifying files on the command line, create or use a tsconfig.json file:
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}Then compile with:
tsc
# or
tsc --project tsconfig.jsonThe tsconfig.json file tells TypeScript which files to compile and where to output them.
If you're using glob patterns, ensure they match .ts or .tsx files:
# These patterns work with --outDir
tsc --outDir dist src/**/*.ts
tsc --outDir dist *.tsx
tsc --outDir dist app.ts utils.ts
# These will cause errors (no .ts files)
tsc --outDir dist *.js
tsc --outDir dist src/**/* # Might match non-TypeScript filesUse explicit .ts/.tsx extensions or ensure your glob patterns only match TypeScript files.
If using tsconfig.json, verify your include patterns match TypeScript files:
{
"compilerOptions": {
"outDir": "dist"
},
"include": [
"src/**/*.ts", // Explicit .ts files
"src/**/*.tsx" // Explicit .tsx files
],
"exclude": [
"node_modules",
"**/*.spec.ts", // Test files if needed
"**/*.test.ts"
]
}Common issues:
- "include": ["src"] - includes directories but not file patterns
- "include": ["src/**/*"] - might include non-TypeScript files
- Missing include/exclude sections entirely
When you have multiple tsconfig files or need to specify a configuration:
# Use a specific tsconfig file
tsc --project tsconfig.prod.json
# Compile from a directory containing tsconfig.json
tsc --project ./src
# Build mode for project references
tsc --build --verboseThe --project flag tells TypeScript to use the specified tsconfig.json file or directory containing one.
Advanced notes
## Understanding TypeScript Compilation Modes
TypeScript has two main compilation modes:
1. File-based compilation: When you specify files on the command line, TypeScript ignores tsconfig.json and compiles only those files. This mode requires explicit file specifications when using --outDir.
2. Project-based compilation: When you use tsconfig.json (either implicitly or with --project), TypeScript uses the configuration to determine which files to compile.
## Common Pitfalls
- Mixed file types: Specifying both .ts and .js files with --outDir will fail because .js files aren't compiled.
- Empty directories: If your include patterns don't match any files, you'll get a different error about no inputs.
- Symlinks and aliases: Complex project structures with symlinks might cause unexpected behavior.
## Alternative Approaches
If you need to output compiled JavaScript to a specific directory without specifying source files, consider:
- Using a build tool like Webpack, Rollup, or esbuild with TypeScript integration
- Creating a shell script that finds all .ts files and passes them to tsc
- Using the TypeScript compiler API programmatically
Frequently asked questions
- What does "Cannot use --outDir without specifying file names with .ts extensions" mean?
- When you run the TypeScript compiler (tsc) with the --outDir flag, you're telling it to output compiled JavaScript files to a specific directory. However, the compiler needs to know which TypeScript source files to compile. This error typically happens in two scenarios: 1.
- How do I fix "Cannot use --outDir without specifying file names with .ts extensions"?
- This TypeScript compilation error occurs when you use the --outDir flag without providing TypeScript source files (.ts or .tsx extensions). The TypeScript compiler needs source files to determine what to compile and where to output the JavaScript files.
- What causes "Cannot use --outDir without specifying file names with .ts extensions"?
- Running tsc --outDir dist without specifying any .ts files on the command line Using tsc --outDir dist with only JavaScript (.js) files specified Incorrect tsconfig.json configuration that doesn't include any TypeScript source files
- Is the "Cannot use --outDir without specifying file names with .ts extensions" error serious?
- It is a low-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐TypeScript Official Documentation
- ๐Typescriptlang.org - outDir.html(Official Docs)
- ๐Typescriptlang.org - compiler-options.html(Official Docs)
- ๐Typescriptlang.org - tsconfig-json.html(Official Docs)
- ๐Github.com - 16749(GitHub)
- ๐ฌStackoverflow.com - typescript-outdir-without-ts-extensions(Stack Overflow)