This TypeScript error occurs when the 'include' field in tsconfig.json has patterns that don't match any files, or when the patterns are empty. TypeScript needs at least one matching file to compile, so this error prevents compilation until you fix the include patterns to match your source files.
The "The 'include' pattern is empty or matches no files" error appears when TypeScript's compiler cannot find any files to compile based on the patterns specified in the 'include' field of your tsconfig.json file. TypeScript uses the 'include' field to determine which files should be part of the compilation. When you run `tsc` or your build process, TypeScript scans for files matching these patterns. If no files match, TypeScript has nothing to compile and throws this error. This typically happens when: 1. The include patterns are incorrect or don't match your project structure 2. You've moved or renamed files without updating tsconfig.json 3. The patterns use incorrect wildcards or directory paths 4. You're using an empty array `[]` for include when you also haven't specified a 'files' array The error is TypeScript's way of saying "I looked for files to compile based on your instructions, but found nothing."
First, examine your tsconfig.json to see what patterns are configured:
{
"compilerOptions": {
// ... other options
},
"include": ["src/**/*", "tests/**/*"],
"exclude": ["node_modules", "dist"]
}Verify the patterns match your project structure. Common patterns include:
- "src/**/*" - All files in src directory and subdirectories
- "**/*.ts" - All .ts files in any directory
- "**/*.{ts,tsx}" - All .ts and .tsx files
- ["src"] - All supported files in src directory
Check if the directories exist:
# Check if src directory exists
ls -la src/
# Check what files are in src
find src -name "*.ts" -o -name "*.tsx" | head -10If your source files are in a different location, update the include patterns:
// WRONG - src directory doesn't exist
{
"include": ["src/**/*"]
}
// CORRECT - use actual directory name
{
"include": ["lib/**/*"]
}
// If using multiple source directories:
{
"include": ["packages/**/*", "apps/**/*", "shared/**/*"]
}
// If source files are in root directory:
{
"include": ["**/*.ts", "**/*.tsx"]
}Verify your project structure:
# Show directory structure
tree -I 'node_modules|dist|build' -L 3
# Or with find
find . -type f -name "*.ts" -o -name "*.tsx" | grep -v node_modules | head -20Update tsconfig.json to match your actual file locations.
TypeScript only includes files with specific extensions by default. Make sure your patterns match:
// Default extensions TypeScript looks for:
// .ts, .tsx, .d.ts
// .js and .jsx if allowJs is true
// WRONG - looking for .typescript files
{
"include": ["**/*.typescript"]
}
// CORRECT - looking for .ts files
{
"include": ["**/*.ts"]
}
// Include both .ts and .tsx
{
"include": ["**/*.{ts,tsx}"]
}
// Include JavaScript files too
{
"compilerOptions": {
"allowJs": true
},
"include": ["**/*.{ts,tsx,js,jsx}"]
}Check what file extensions you actually have:
# Count files by extension
find . -type f -name "*.ts" | wc -l
find . -type f -name "*.tsx" | wc -l
find . -type f -name "*.js" | wc -l
find . -type f -name "*.jsx" | wc -lUpdate patterns to match your actual file extensions.
If you have a small number of files or want explicit control, use the 'files' array instead of 'include':
// Using files array (explicit file listing)
{
"compilerOptions": {
// ... options
},
"files": [
"src/index.ts",
"src/utils/helpers.ts",
"src/types/index.d.ts"
]
}
// Mixed approach - include patterns plus specific files
{
"include": ["src/**/*"],
"files": [
"global.d.ts", // Always include this
"types/custom.d.ts" // And this
]
}
// When both files and include are specified:
// - files are always included
// - include patterns add additional files
// - exclude still applies to include patternsThe 'files' array is useful when:
- You have a small, fixed set of entry points
- You want to exclude test files from compilation
- You need to ensure specific files are always compiled
- Debugging include/exclude pattern issues
Note: If you specify 'files', the default value for 'include' becomes [] (empty array).
The 'exclude' field can accidentally exclude all files. Check for conflicts:
// WRONG - exclude pattern removes everything
{
"include": ["src/**/*"],
"exclude": ["src"] // Excludes entire src directory!
}
// CORRECT - exclude specific subdirectories
{
"include": ["src/**/*"],
"exclude": ["src/**/*.test.ts", "src/**/*.spec.ts"]
}
// WRONG - exclude uses wildcard that matches everything
{
"include": ["**/*.ts"],
"exclude": ["**/*"] // Excludes all files!
}
// CORRECT - exclude node_modules and build outputs
{
"include": ["**/*.ts"],
"exclude": ["node_modules", "dist", "build", "coverage"]
}Debug what files are being included/excluded:
# Create a test to see what files match
npx tsc --listFiles | head -20
# Or compile with verbose output
npx tsc --extendedDiagnosticsRemember: exclude only affects files found via include patterns, not files specified in the files array.
If using multiple tsconfig files or extends, verify the configuration hierarchy:
// Base tsconfig.json
{
"compilerOptions": {
"strict": true
},
"include": ["src/**/*"]
}
// tsconfig.build.json (extends base)
{
"extends": "./tsconfig.json",
"include": ["src/**/*"], // This OVERRIDES parent include!
"exclude": ["**/*.test.ts", "**/*.spec.ts"]
}
// When using extends:
// - compilerOptions are merged
// - include/exclude/files REPLACE parent values (not merged)
// - Last config in chain wins
// Common mistake - empty include in child config
{
"extends": "./tsconfig.json",
"include": [], // Overrides parent with empty array!
"exclude": ["tests"]
}Check which tsconfig file you're using:
# Default uses tsconfig.json
npx tsc
# Specify config file
npx tsc -p tsconfig.build.json
# Check current directory
pwd
ls -la tsconfig*.jsonEnsure your build command references the correct tsconfig file.
### Understanding TypeScript File Inclusion
TypeScript uses three mechanisms to determine which files to compile:
1. files: Explicit array of file paths. Always included.
2. include: Glob patterns matching files. Defaults to **/* if 'files' not specified.
3. exclude: Patterns to remove from 'include' results.
Important behaviors:
- If 'files' is specified, 'include' defaults to [] (empty array)
- If 'files' is not specified, 'include' defaults to **/* (all files)
- 'exclude' only affects files found via 'include', not 'files'
- Patterns in child configs (using 'extends') replace parent patterns
### Debugging File Inclusion
Use these commands to understand what's happening:
# List all files TypeScript would compile
npx tsc --listFiles
# Show detailed diagnostics
npx tsc --extendedDiagnostics
# Show compiler output with file list
npx tsc --listFiles --noEmit
# Check a specific config file
npx tsc -p tsconfig.build.json --listFiles### Common Pattern Examples
// Monorepo structure
{
"include": [
"packages/*/src/**/*",
"packages/*/index.ts"
],
"exclude": [
"packages/*/node_modules",
"packages/*/dist",
"packages/*/test"
]
}
// React project with tests
{
"include": ["src/**/*"],
"exclude": [
"src/**/*.test.ts",
"src/**/*.test.tsx",
"src/**/*.spec.ts",
"src/**/*.spec.tsx",
"src/setupTests.ts"
]
}
// Library with declaration files
{
"include": ["src/**/*.ts"],
"files": [
"index.d.ts", // Always include
"types/global.d.ts" // Global type declarations
]
}
// Mixed TypeScript/JavaScript project
{
"compilerOptions": {
"allowJs": true
},
"include": ["src/**/*.{ts,tsx,js,jsx}"],
"exclude": ["src/**/*.test.*"]
}### TypeScript Version Differences
- TypeScript 2.0+: Introduced 'include'/'exclude' patterns
- TypeScript 3.0+: Improved pattern matching performance
- Recent versions: Better error messages for empty include patterns
### Integration with Build Tools
Webpack with ts-loader:
module.exports = {
module: {
rules: [
{
test: /.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
};Note: ts-loader uses TypeScript's configuration, so include/exclude patterns apply.
Vite:
// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
})Vite uses esbuild for TypeScript transpilation and respects tsconfig.json.
### Empty Include in CI/CD
Common CI/CD issues:
- Build runs from different directory than expected
- Source code checkout location differs
- Docker container has different file structure
- Build cache causes stale configuration
Solution: Use absolute paths or verify working directory:
# In CI script
pwd
ls -la
npx tsc --listFilesFunction 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