This TypeScript configuration error occurs when your tsconfig.json's 'include' and 'exclude' patterns don't match any TypeScript source files (.ts/.tsx). The compiler has nothing to compile, causing build failures. Fixes involve adjusting file patterns, checking file extensions, or verifying project structure.
The "no source files found" error appears when TypeScript's compiler examines your tsconfig.json configuration and finds that the combination of 'include' and 'exclude' patterns results in zero TypeScript source files to compile. This typically happens because: 1. **File patterns are too restrictive**: The 'include' pattern doesn't match your actual source files 2. **Wrong file extensions**: Your files might have different extensions than expected 3. **Exclude patterns are too broad**: The 'exclude' pattern might be removing all your source files 4. **Project structure mismatch**: Your source files might be in a different location than configured TypeScript needs at least one .ts or .tsx file to compile. When it finds none, it reports this error to prevent empty or meaningless compilation. This is a safety check to ensure you're compiling what you intend to compile.
First, examine your tsconfig.json to understand what patterns are configured:
{
"compilerOptions": {
// ... other options
},
"include": ["src/**/*"], // This should match your source files
"exclude": ["node_modules", "dist", "build"] // This shouldn't exclude your source files
}Common patterns:
- ["src/**/*"] - All files in src directory recursively
- ["src/**/*.ts", "src/**/*.tsx"] - Only TypeScript files in src
- ["**/*.ts", "**/*.tsx"] - All TypeScript files in project (except excluded)
- ["."] - Current directory (includes everything)
Check if your patterns match your actual file structure:
# List files that would be included
find . -name "*.ts" -o -name "*.tsx" | head -20
# Check if src directory exists and has TypeScript files
ls -la src/ 2>/dev/null || echo "src directory doesn't exist"Adjust the include pattern based on your project structure:
If your source files are in src/:
{
"include": ["src/**/*.ts", "src/**/*.tsx"]
}If your source files are in multiple directories:
{
"include": ["src/**/*.ts", "src/**/*.tsx", "lib/**/*.ts", "tests/**/*.ts"]
}If you have a flat structure (files in project root):
{
"include": ["*.ts", "*.tsx"]
}For Next.js projects (pages and components in different places):
{
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"]
}After updating tsconfig.json, test the pattern:
# Using find to simulate TypeScript's pattern matching
find . -path ./node_modules -prune -o -name "*.ts" -print | head -10The exclude pattern might be too broad. Check what you're excluding:
{
"exclude": ["node_modules", "dist", "build", "coverage"]
}Common issues:
1. Excluding src/ by accident: "exclude": ["node_modules", "src"] would exclude all source files
2. Wildcard exclusions: "exclude": ["**/*.test.ts"] might be too broad if it matches your source files
3. Relative path issues: Patterns might not work as expected with different working directories
Temporarily comment out exclude to test:
{
"include": ["src/**/*.ts", "src/**/*.tsx"],
// "exclude": ["node_modules", "dist"] // Comment out to test
}If compilation works without exclude, gradually add back exclusions one by one to find the problematic pattern.
TypeScript only compiles .ts and .tsx files by default. Check your file extensions:
# Check what TypeScript files exist
find . -type f ( -name "*.ts" -o -name "*.tsx" ) | wc -l
# If zero, you might have:
# - .js/.jsx files (JavaScript, not TypeScript)
# - .mts/.cts files (ESM/CJS TypeScript modules)
# - No TypeScript files at all
# Check for JavaScript files that should be TypeScript
find . -type f -name "*.js" -o -name "*.jsx" | head -10If you have .mts or .cts files (ESM/CJS TypeScript modules), update include:
{
"include": ["**/*.ts", "**/*.tsx", "**/*.mts", "**/*.cts"]
}If you only have JavaScript files, you need to:
1. Rename .js/.jsx files to .ts/.tsx
2. Or configure TypeScript to allow JS files:
{
"compilerOptions": {
"allowJs": true
},
"include": ["**/*.js", "**/*.jsx"]
}TypeScript resolves patterns relative to the tsconfig.json location. Verify:
1. tsconfig.json is in the right place: Usually in project root
2. Using extends correctly: If extending another config, patterns might be relative to that file
3. Multiple tsconfig.json files: You might have nested configs (like in a monorepo)
# Find all tsconfig.json files
find . -name "tsconfig.json" -type f
# Check current directory vs tsconfig location
pwd
cat tsconfig.json | grep -A5 -B5 "include"If using extends, patterns are resolved relative to the base config:
{
"extends": "../../tsconfig.base.json",
"include": ["src/**/*"] // Relative to THIS tsconfig.json, not the base
}For monorepos with package-specific configs:
// packages/app/tsconfig.json
{
"extends": "../../tsconfig.base.json",
"include": ["src/**/*", "../../shared/**/*"] // Need to include shared files
}TypeScript provides tools to debug configuration issues:
# Show which files TypeScript would compile
npx tsc --listFiles
# Show detailed compilation info
npx tsc --diagnostics
# Show project configuration
npx tsc --showConfig
# Trace why files are included/excluded
npx tsc --traceResolutionExample output analysis:
Files: 0
Total lines: 0
Total files: 0This confirms no files are being compiled.
You can also create a minimal test:
# Create a test TypeScript file
echo 'console.log("test");' > test.ts
# Update tsconfig to include it
echo '{"include": ["test.ts"]}' > tsconfig.test.json
# Try to compile
npx tsc -p tsconfig.test.jsonIf this works, your original patterns are the issue. If it fails, there might be a deeper TypeScript installation or configuration problem.
### Understanding TypeScript's File Resolution
TypeScript uses glob patterns similar to .gitignore but with some differences:
1. Pattern interpretation:
- * matches any characters except /
- **/ matches zero or more directories
- ? matches any single character except /
- [abc] matches any character in the set
- {a,b} matches either pattern a or b
2. Order matters: Patterns are evaluated in order, but TypeScript might optimize this
3. Default includes: If no include is specified, TypeScript includes all files in the containing directory and subdirectories except those excluded
### Common Pitfalls with Modern TypeScript Features
ESM/CJS Module Extensions:
- .mts / .cts files are TypeScript module files
- Need explicit inclusion: "include": ["**/*.ts", "**/*.tsx", "**/*.mts", "**/*.cts"]
- Or use: "include": ["**/*"] and rely on TypeScript to filter
Declaration Files:
- .d.ts files are included by default if they're referenced
- But they don't count as "source files" for compilation
- A project with only .d.ts files will show this error
Composite Projects:
When using composite: true or project references:
{
"compilerOptions": {
"composite": true
},
"references": [
{ "path": "../shared" }
]
}The referenced project's files are automatically included.
### TypeScript Version Differences
Different TypeScript versions handle patterns slightly differently:
- TypeScript 4.0+: Better handling of ** patterns
- TypeScript 4.7+: Native support for .mts/.cts
- Always test with your project's specific TypeScript version
### Alternative: files Property
Instead of include/exclude, you can explicitly list files:
{
"files": [
"src/index.ts",
"src/utils.ts",
"src/types.ts"
]
}This guarantees specific files are compiled but doesn't scale well for large projects.
### Integration with Build Tools
Webpack/Vite: These tools often have their own file resolution that might conflict with TypeScript's patterns. Ensure consistency between:
- TypeScript's include/exclude
- Webpack's entry points
- Vite's root configuration
Monorepo Tools (Turborepo, Nx, Lerna):
- Each package might have its own tsconfig.json
- Root tsconfig.json might use "references"
- Ensure patterns account for cross-package dependencies
### Debugging Script
Create a debug script to analyze your configuration:
// debug-tsconfig.js
const ts = require('typescript');
const path = require('path');
const configPath = ts.findConfigFile('./', ts.sys.fileExists);
if (!configPath) {
console.error('No tsconfig.json found');
process.exit(1);
}
const config = ts.readConfigFile(configPath, ts.sys.readFile);
const parsed = ts.parseJsonConfigFileContent(
config.config,
ts.sys,
path.dirname(configPath)
);
console.log('Root files:', parsed.fileNames.length);
console.log('First 10 files:', parsed.fileNames.slice(0, 10));Run with: node debug-tsconfig.js
Function expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript
Type narrowing from typeof check produces 'never'
How to fix "Type narrowing produces never" in TypeScript
Type parameter 'T' has conflicting constraints
How to fix "Type parameter has conflicting constraints" in TypeScript