This TypeScript error occurs when the compiler cannot find any source files matching the patterns specified in your tsconfig.json's 'include' or 'files' properties. The fix involves correcting file patterns, verifying file paths, or adjusting your TypeScript configuration to include the correct source files.
The "Cannot find input files matching pattern" error appears when TypeScript's compiler looks for source files to compile but finds none matching the patterns you've specified in your tsconfig.json configuration. This typically happens when: 1. **Incorrect glob patterns**: The patterns in your 'include' array don't match any actual files 2. **Missing files**: The files referenced in your 'files' array don't exist at the specified paths 3. **Wrong directory**: Your tsconfig.json is in the wrong location relative to your source files 4. **Empty project**: There are no .ts/.tsx files in the directories you're trying to include TypeScript needs at least one input file to compile. When it can't find any files matching your configuration, it shows this error to prevent compiling nothing (which would be confusing). This is a configuration issue rather than a code error, and fixing it involves adjusting your tsconfig.json or project structure. The error commonly appears when: - Starting a new TypeScript project with an empty src/ directory - Moving files around without updating tsconfig.json - Using incorrect glob patterns that don't match your file structure - Having typos in file paths or directory names
First, examine your tsconfig.json's 'include' property. The default is ["**/*"] which matches all files, but if you've customized it, verify the patterns:
// Example tsconfig.json
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs"
},
"include": ["src/**/*.ts", "src/**/*.tsx"]
}Test if your patterns match files:
# Check what files match your patterns
# For pattern "src/**/*.ts"
find src -name "*.ts" | head -5
# For pattern "**/*.ts"
find . -name "*.ts" | head -5
# If no files found, you need to create TypeScript files
# or adjust your patternsCommon pattern issues:
- "src/*" only matches files directly in src/, not subdirectories
- "src/**" matches directories but not files (needs "src/**/*")
- Missing file extensions: "src/**/*" includes all files, not just .ts/.tsx
- Case sensitivity: "Src" vs "src" on Linux/macOS
If you're using the 'files' array instead of 'include', check each path exists:
{
"compilerOptions": { /* ... */ },
"files": [
"src/index.ts",
"src/utils/helpers.ts",
"src/types.d.ts"
]
}Verify each file exists:
# Check each file in the files array
ls -la src/index.ts
ls -la src/utils/helpers.ts
ls -la src/types.d.ts
# If any file is missing, either:
# 1. Create the missing file
# 2. Remove it from the files array
# 3. Fix the path if it's incorrectImportant notes about 'files':
- Paths are relative to tsconfig.json location
- Only these exact files will be compiled (no glob patterns)
- Useful for small projects or explicit file lists
- Consider switching to 'include' for flexibility
Create a minimal TypeScript file to test if your configuration works:
# Create a test directory and file
mkdir -p src
cat > src/test.ts << 'EOF'
// Test TypeScript file
const message: string = "Hello TypeScript";
console.log(message);
EOFThen update your tsconfig.json to include this file:
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"outDir": "./dist"
},
"include": ["src/**/*.ts"]
}Try compiling:
npx tsc
# If successful, you should see:
# - No error messages
# - dist/test.js created (if outDir is set)
# - Or test.js in same directory (if no outDir)
# If still failing, the issue is with tsconfig.json location
# or other configurationTypeScript resolves paths relative to the tsconfig.json file. Ensure it's in the correct location:
# Check current directory and tsconfig location
pwd
ls -la tsconfig.json
# If tsconfig.json is in a subdirectory, TypeScript will look
# for files relative to that subdirectory
# Example structure:
# project/
# ├── tsconfig.json # Looking for src/ relative to project/
# └── src/
# └── index.ts # ✓ Found: project/src/index.ts
# WRONG structure:
# project/
# ├── config/
# │ └── tsconfig.json # Looking for src/ relative to config/
# └── src/
# └── index.ts # ✗ Not found: config/src/index.ts doesn't existSolutions:
1. Move tsconfig.json to project root
2. Adjust paths in tsconfig.json to be relative to its location
3. Use multiple tsconfig.json files with "extends" for subdirectories
For complex projects, you can use project references:
// tsconfig.json at project root
{
"compilerOptions": {
"composite": true
},
"references": [
{ "path": "./packages/client" },
{ "path": "./packages/server" }
]
}
// packages/client/tsconfig.json
{
"extends": "../../tsconfig.base.json",
"include": ["src/**/*.ts"]
}The 'exclude' property can accidentally exclude all files. Check your configuration:
{
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "dist", "**/*.test.ts"]
}Common exclude mistakes:
- "exclude": ["src"] - excludes entire src directory
- "exclude": ["**/*"] - excludes everything
- "exclude": ["./*"] - excludes files in current directory
Debug by temporarily removing exclude:
{
"include": ["src/**/*.ts"],
// "exclude": ["node_modules", "dist"] // Comment out to test
}If compilation works without exclude, your exclude pattern is too broad.
Add patterns back one at a time to find the problematic one.
Remember: 'exclude' only affects files found via 'include', not files imported via 'files' array.
TypeScript can show detailed information about why files are included/excluded:
{
"compilerOptions": {
"explainFiles": true
},
"include": ["src/**/*.ts"]
}Run TypeScript with this option:
npx tsc --explainFiles
# Output shows:
# - Files being included and why
# - Files being excluded and why
# - Configuration being used
# - Resolution details
# Example output:
# File '/project/src/index.ts':
# Matched by include pattern '**/*' in 'tsconfig.json'
# File '/project/node_modules/typescript/lib/lib.es5.d.ts':
# Library code fileThis helps identify:
- Which patterns are matching (or not matching)
- Why certain files are excluded
- Configuration issues
You can also use traceResolution for module resolution debugging:
npx tsc --traceResolution### Understanding TypeScript's File Inclusion System
TypeScript uses a multi-step process to determine which files to compile:
1. Explicit files list (files array): Exact file paths, no glob patterns
2. Include patterns (include array): Glob patterns, defaults to ["**/*"]
3. Exclude patterns (exclude array): Removes files from include matches
4. Imported files: Files referenced via import/require//// <reference>
5. Type declaration files: Automatic inclusion of @types packages
### Glob Pattern Reference
| Pattern | Meaning | Example Matches |
|---------|---------|-----------------|
| * | Any string except / | file.ts, test.ts |
| ? | Any single character except / | a.ts, b.ts |
| **/ | Zero or more directories | src/a.ts, src/lib/b.ts |
| {a,b} | Either a or b | {src,test}/*.ts |
| [abc] | Any character in set | [abc].ts matches a.ts, b.ts, c.ts |
| [^abc] | Any character NOT in set | [^abc].ts matches d.ts, e.ts |
### Common tsconfig.json Patterns
Basic project:
{
"include": ["src/**/*"]
}React project:
{
"include": ["src/**/*.ts", "src/**/*.tsx"]
}Monorepo structure:
{
"include": ["packages/*/src/**/*.ts"],
"exclude": ["packages/*/node_modules", "packages/*/dist"]
}Test files separate:
{
"include": ["src/**/*.ts"],
"exclude": ["**/*.test.ts", "**/*.spec.ts"]
}### TypeScript Configuration Inheritance
You can extend a base configuration:
// tsconfig.base.json
{
"compilerOptions": {
"strict": true,
"target": "es2020"
}
}
// tsconfig.json
{
"extends": "./tsconfig.base.json",
"include": ["src/**/*.ts"]
}
// tsconfig.test.json
{
"extends": "./tsconfig.json",
"include": ["src/**/*.ts", "tests/**/*.ts"]
}### IDE-Specific Considerations
VS Code:
- Uses the nearest tsconfig.json
- Restart TS server after config changes: Ctrl+Shift+P > TypeScript: Restart TS server
- Can have workspace-specific settings
WebStorm/IntelliJ:
- May cache TypeScript configuration
- Check "TypeScript" settings in Preferences
- May need to mark directories as "TypeScript Sources Root"
Build Tools Integration:
- Webpack: Uses ts-loader or babel-loader with @babel/preset-typescript
- Vite: Built-in TypeScript support, respects tsconfig.json
- esbuild: Fast but may have different behavior with certain tsconfig options
### Empty Projects and Starter Templates
If starting from scratch, consider using:
# Official TypeScript starter
npx tsc --init
# Creates tsconfig.json with common options
# Then create your source files
# Or use a template:
npx create-react-app my-app --template typescript
npx @vitejs/plugin-react my-app --template react-ts### Debugging Complex Configurations
For complex setups, create a minimal reproduction:
# Create test directory
mkdir ts-test && cd ts-test
# Minimal tsconfig.json
echo '{"include": ["src/**/*.ts"]}' > tsconfig.json
# Minimal source file
mkdir src
echo 'const x: string = "test";' > src/test.ts
# Test compilation
npx tsc
# If this works, gradually add your actual configuration
# until you find what breaks itFunction 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