Cannot find module
TypeScript could not locate a module that is being imported.
- 1The path to the module is incorrect.
- 2The module name has a typo.
- 3The module exists, but its type declarations are missing and it is not a plain JavaScript module.
An import statement references a file that does not exist.
import { a } from "./non-existent-file";expected output
error TS2307: Cannot find module './non-existent-file' or its corresponding type declarations.
Fix 1
Correct the module path
WHEN The path to the file is wrong.
import { a } from "./existent-file";Why this works
Providing the correct relative or absolute path to the module file.
Fix 2
Install the module and its types
WHEN The module is an external dependency that is not installed.
// First run: npm install <module-name>
// Then in your code:
import { a } from "<module-name>";Why this works
Installing the package makes it available to the module resolution system.
✕ Create an empty module declaration with 'declare module'
This silences the error but provides no type safety, effectively treating the module as 'any'.
microsoft/TypeScript src/compiler/diagnosticMessages.json
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev