What is TS2307?
TS2307 occurs when TypeScript cannot locate a module you're trying to import. This can happen for several reasons: the package isn't installed, type definitions are missing, or the path is incorrect.
// Example of TS2307 error import axios from 'axios'; // Error: Cannot find module 'axios' import { helper } from './utils/helper'; // Error: Cannot find module './utils/helper'
Common Causes
1. Package Not Installed
// โ Package not in node_modules import lodash from 'lodash'; // Error! // โ Install the package // npm install lodash // npm install --save-dev @types/lodash
2. Missing Type Definitions
// Package exists but has no built-in types import express from 'express'; // Error: Could not find type declarations // โ Install types separately // npm install --save-dev @types/express
3. Incorrect Relative Path
// File structure: // src/ // components/ // Button.tsx // utils/ // helper.ts // In Button.tsx: // โ Wrong path import { helper } from './helper'; // Error! // โ Correct relative path import { helper } from '../utils/helper';
4. Missing File Extension
// Some configurations require extensions // โ Might not work import { config } from './config'; // โ With extension (for ESM or specific configs) import { config } from './config.js'; // Note: Use .js even for .ts files in some ESM setups
5. Path Alias Not Configured
// โ Using alias without configuration import { Button } from '@/components/Button'; // Error! // โ Configure paths in tsconfig.json first
6. CSS/Asset Imports
// โ TypeScript doesn't understand non-JS imports by default import styles from './styles.css'; // Error! import logo from './logo.png'; // Error!
Solutions
1. Install the Package
# Install the package npm install package-name # If types are separate npm install --save-dev @types/package-name
2. Configure Path Aliases
// tsconfig.json { "compilerOptions": { "baseUrl": ".", "paths": { "@/*": ["src/*"], "@components/*": ["src/components/*"], "@utils/*": ["src/utils/*"] } } } // Now you can use: import { Button } from '@components/Button';
3. Create Type Declaration for Assets
// src/types/assets.d.ts declare module '*.css' { const content: { [className: string]: string }; export default content; } declare module '*.png' { const src: string; export default src; } declare module '*.svg' { import * as React from 'react'; export const ReactComponent: React.FC<React.SVGProps<SVGSVGElement>>; const src: string; export default src; }
4. Create Type Declaration for Untyped Package
// src/types/untyped-package.d.ts declare module 'untyped-package' { export function someFunction(param: string): void; export interface SomeInterface { prop: string; } export default class MainClass { constructor(options: object); } } // Or for quick fix (not recommended for production) declare module 'untyped-package';
5. Check Module Resolution
// tsconfig.json { "compilerOptions": { // For Node.js projects "moduleResolution": "node", // For modern bundlers (Vite, etc.) "moduleResolution": "bundler", // For Node.js 16+ with ESM "moduleResolution": "node16" } }
๐ก Pro Tip
Use npm ls package-name to verify if a package is actually installed, and check if it's in the correct location (dependencies vs devDependencies).
Real-World Examples
React + TypeScript Setup
// Required packages for React TypeScript project npm install react react-dom npm install --save-dev typescript @types/react @types/react-dom // tsconfig.json { "compilerOptions": { "jsx": "react-jsx", "moduleResolution": "bundler", "esModuleInterop": true } }
Next.js Path Aliases
// tsconfig.json for Next.js { "compilerOptions": { "baseUrl": ".", "paths": { "@/*": ["./*"] } } } // Usage import { Button } from '@/components/Button'; import { api } from '@/lib/api';
Monorepo Package Resolution
// In a monorepo, reference other packages // tsconfig.json { "compilerOptions": { "paths": { "@myorg/shared": ["../../packages/shared/src"], "@myorg/ui": ["../../packages/ui/src"] } }, "references": [ { "path": "../../packages/shared" }, { "path": "../../packages/ui" } ] }
โ ๏ธ Common Gotcha
After adding type definitions or modifying tsconfig.json, restart your TypeScript server. In VS Code, use Cmd/Ctrl + Shift + P โ "TypeScript: Restart TS Server".
Summary
- TS2307 means TypeScript can't locate a module
- Check if the package is installed (npm ls)
- Install @types/* for packages without built-in types
- Verify relative import paths are correct
- Configure path aliases in tsconfig.json
- Create .d.ts files for untyped packages or assets
- Check moduleResolution setting matches your setup