error TS2307: Cannot find module 'x' or its corresponding type declarations: how to fix it
error TS2307: Cannot find module 'x' or its corresponding type declarations means the TypeScript compiler could not find a file or type declarations for an import. The usual causes: the package is not installed, an @/ path alias missing from paths in tsconfig.json, a moduleResolution setting that does not match how the package or your imports are written, and .css, .svg or image imports with no declaration. It is a compile-time error from tsc, not Node.js's runtime Cannot find module or a bundler's Can't resolve.
By Livops team, the people who build and run LivopsUpdated
What tsc prints in a build log (the file, line and column come first):
src/index.ts(1,19): error TS2307: Cannot find module 'zod' or its corresponding type declarations.
src/routes.ts(4,25): error TS2307: Cannot find module '@/lib/db' or its corresponding type declarations.
src/App.tsx(3,18): error TS2307: Cannot find module './logo.svg' or its corresponding type declarations.
This page explains what the compiler was looking for, how TS2307 differs from Node.js’s and a bundler’s “cannot find” errors, the five causes with the fix for each, and where it shows on Livops.
What does the error mean?
TS2307’s message is Cannot find module 'x' or its corresponding type declarations., with the import in place of x[1]: for that import, the compiler found neither a TypeScript file nor type declarations. How it looks depends on moduleResolution. For a bare name such as 'zod', it looks in node_modules and, if a folder of the same name exists in node_modules/@types, resolves types from there[3]. Under node16, nodenext and bundler it also follows a package’s "exports" in package.json, matching the "types" condition[3].
Read the quoted name first:
- A bare name (
'zod','express'): a package. Cause 1, 2 or 4. - An alias (
'@/lib/db','~/utils'): a path mapping. Cause 3. - A relative path (
'./utils'): a file of yours. Check the path and the case of the name, then cause 4. - A non-code file (
'./logo.svg','./styles.module.css'): cause 5.
TS2307, Cannot find module, Can’t resolve: three tools, three moments
| Message | Printed by | When |
|---|---|---|
error TS2307: Cannot find module 'x' or its corresponding type declarations. |
The TypeScript compiler, tsc |
While checking types, before the code runs |
Error: Cannot find module 'x' (MODULE_NOT_FOUND, ERR_MODULE_NOT_FOUND) |
Node.js | When the program runs; see Error: Cannot find module |
Module not found: Can't resolve 'x' |
webpack, Next.js | While bundling; see Module not found: Can’t resolve |
Each tool resolves imports with its own rules, so one passing proves nothing about the others. The paths option, for example, does not change the import paths in the JavaScript tsc emits[3][2]: an alias that satisfies the compiler can still fail in Node.js at runtime. And Vite only transpiles .ts files and does not type-check them[7], so in a Vite project TS2307 appears only when tsc runs, for example tsc --noEmit alongside vite build, as Vite’s docs suggest for production[7].
Neighbouring errors
| Code | Message (from the compiler’s message list) | Where it is covered |
|---|---|---|
| TS7016 | Could not find a declaration file for module 'x'. '/app/node_modules/x/index.js' implicitly has an 'any' type.[1] |
Cause 2 |
| TS2591 | Cannot find name 'fs'. Do you need to install type definitions for node? ...[1] |
Cause 2 |
| TS2835 | Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './utils.js'?[1] |
Cause 4 |
| TS2882 | Cannot find module or type declarations for side-effect import of './global.css'.[1] |
Cause 5 |
| TS5101, TS5107 | an option such as baseUrl or moduleResolution=node10 is deprecated and will stop functioning in TypeScript 7.0[1] |
Causes 3 and 4 |
Causes, most common first
1. The package is not installed
The import names a package that is not in node_modules where the compiler looks: you never added it, it is only installed on your machine, or the install before the build did not run in that folder.
Fix: install it into the project and commit both files:
npm install zod
git add package.json package-lock.json
If it is listed and still missing on the server, the install is the problem, not TypeScript; Error: Cannot find module covers skipped devDependencies and installs that run in another folder.
2. The types are missing
Many packages ship their own declarations; then no @types package is needed[4]. For one that does not, the type declarations usually live in a package with the same name, prefixed with @types/[4]:
npm install --save-dev @types/lodash
For an installed package that has no types, the compiler does not report TS2307 but TS7016 (Could not find a declaration file for module ...)[1], and suggests the fix itself: Try npm i --save-dev @types/untyped-lib if it exists or add a new declaration (.d.ts) file containing declare module 'untyped-lib';[1]. A one-line declare module "untyped-lib"; in a .d.ts file silences it, at the cost of having no real types for that package.
Node.js’s own modules (fs, path, node:crypto) get their types from @types/node. Since TypeScript 6.0, types defaults to [], so @types packages are no longer all included automatically[5][2], and the 6.0 release notes list Cannot find module '...' or its corresponding type declarations among the errors this causes[5]. The compiler’s hint for Node.js names both steps: Try npm i --save-dev @types/node and then add 'node' to the types field in your tsconfig.[1]
npm install --save-dev @types/node
{
"compilerOptions": {
"types": ["node"]
}
}
Once types is set, only the packages listed are included in the global scope[2], so list every one you need ("node", "jest", "vite/client" …). "types": ["*"] restores the behavior of 5.9[5].
3. A path alias that tsconfig.json does not map
import { db } from "@/lib/db" works only if compilerOptions.paths maps @/*. paths entries are resolved relative to baseUrl if it is set, and otherwise relative to the tsconfig.json that defines them[3].
baseUrl is on its way out. TypeScript 6.0 deprecated it: it is no longer a lookup root for module resolution, and the migration is to remove it and add its prefix to the paths entries[5]. TypeScript 7.0 no longer supports it[6]. On 6.0 a config that still has it reports TS5101 unless "ignoreDeprecations": "6.0" is set[5][1]; that setting does not help on 7.0[5]. Write paths without it:
{
"compilerOptions": {
"paths": {
"@/*": ["./src/*"]
}
}
}
Then check two things:
- The tsconfig the build uses. An alias defined in
tsconfig.app.jsondoes not help atsc -p tsconfig.jsonthat never reads it, and atsconfig.jsonthat is not committed does not exist on the server. - The tool after tsc.
pathsonly tells TypeScript about a mapping that another tool already does at runtime or when bundling[2]; it does not change the emitted import paths[3]. A bundler needs its own alias (see Module not found: Can’t resolve); plainnode dist/index.jsdoes not know@/at all and fails withCannot find module.
4. moduleResolution does not match the package or your imports
moduleResolution decides how imports are looked up. node16 and nodenext are for modern Node.js, bundler is for bundlers and, like the Node.js modes, supports "imports" and "exports" in package.json, but never requires file extensions on relative paths[2]. Three mismatches produce TS2307 or its relatives:
-
node10(node) and a package that publishes its types through"exports".node10does not support"exports"[3], so a package whose types are only reachable through"exports"is not found. The compiler adds a hint under the TS2307 line:There are types at '...', but this result could not be resolved under your current 'moduleResolution' setting. Consider updating to 'node16', 'nodenext', or 'bundler'.[1] TypeScript 6.0 deprecatednode10, recommendingnodenextfor code that targets Node.js directly andbundlerfor code that goes through a bundler or Bun[5]; TypeScript 7.0 no longer supports it[6].{ "compilerOptions": { "module": "nodenext", "moduleResolution": "nodenext" } }or, for an app built with Vite, webpack or another bundler:
{ "compilerOptions": { "module": "esnext", "moduleResolution": "bundler" } } -
nodenextand a relative import without an extension. Node.js does not support extensionless relative paths inimport[3], and undernode16andnodenextneither does TypeScript: in an ES module,import { u } from "./utils"fails with TS2835, which names the fix:Did you mean './utils.js'?[1] Write the.jspath even though the file isutils.ts; TypeScript resolves./mod.jsto./mod.ts[3].import { u } from "./utils.js"; // the source file is src/utils.ts -
A deep import that
"exports"does not list. Undernode16,nodenextandbundler, TypeScript follows the package’s"exports"[3], soimport x from "pkg/dist/internal"fails whenexportsdoes not expose that path, even though the file is on disk. Import from the entry points the package documents.
5. Importing .css, .svg or images without a declaration
TypeScript has no types for non-code files, so import logo from "./logo.svg" and import styles from "./App.module.css" fail with TS2307 unless something declares them. Since TypeScript 6.0, noUncheckedSideEffectImports is on by default[5], so a bare import "./global.css" is checked too and fails with TS2882[1].
In a Vite project, add Vite’s client types. They provide types for asset imports such as .svg files and for import.meta.env[7], and declare *.module.css, *.css, *.png and *.svg among others[8]. Either in tsconfig.json[7]:
{
"compilerOptions": {
"types": ["vite/client"]
}
}
or in a vite-env.d.ts file[7]:
/// <reference types="vite/client" />
Elsewhere, declare the patterns yourself. A pattern ambient module has one * wildcard and matches any import path that fits[3]:
// src/assets.d.ts
declare module "*.svg" {
const src: string;
export default src;
}
declare module "*.module.css" {
const classes: Record<string, string>;
export default classes;
}
declare module "*.css" {}
The .d.ts file must be inside what include covers. The declarations only describe the imports to TypeScript; the bundler still has to know how to load the files.
How to confirm the fix locally
Run the compiler exactly as the build does, on a clean install:
rm -rf node_modules
npm ci
npx tsc --noEmit # or the build script: npm run build
To see where the compiler looked for a module, set traceResolution; TypeScript then prints its resolution process for each file[2]:
npx tsc --noEmit --traceResolution | grep -A20 "'@/lib/db'"
Then run the built app or the bundler once: TS2307 gone does not mean Node.js or the bundler will find the same import.
On Livops
-
Where it shows. TS2307 stops the build: the deploy fails as
build_failed, and the previous version keeps running. Livops’s own docs name it: lines likeerror TS2307mean a type error in the code; runnpm run buildlocally, fix it and push. See Build failed. -
The assistant. If a build fails, the log shows why, and the assistant explains it.
-
Install dev dependencies for the build. If
typescriptand@types/*are devDependencies, the install command must include them. Set the commands in Settings → Install and Settings → Build, or inlivops.json, whereinstallandbuildare applied on every deploy:{ "install": "npm ci --include=dev", "build": "npm run build" } -
Uploaded folders. When you deploy by uploading a folder,
node_modulesis dropped, so types that exist only in your localnode_modulesdo not arrive. They must be inpackage.json. -
TypeScript at runtime. For a TypeScript server, build first and start the built file, for example
"start": "node dist/main.js". See No start command.
If the build passes and the app then stops on Error: Cannot find module, see Error: Cannot find module. If it says Cannot use import statement outside a module, see that page. If the build stops because tsc itself is not found, see sh: tsc: not found.
Questions
What is the difference between TS2307 and Node's Cannot find module?
TS2307 comes from the TypeScript compiler while it checks types, before any code runs. Node.js's Error: Cannot find module with code MODULE_NOT_FOUND comes from the running program. A bundler's Module not found: Can't resolve comes from webpack or Next.js while it bundles. Each tool resolves imports with its own rules, so one can pass while another fails.
The package is installed. Why does TypeScript still not find it?
Usually the moduleResolution setting. With the old node10 resolution TypeScript ignores package.json exports, so a package that publishes its types only through exports is not found, and the error adds There are types at ... but this result could not be resolved under your current moduleResolution setting. Use nodenext for Node.js apps or bundler for bundled apps.
Do I need to install @types packages?
Only for packages that ship no types of their own. Install them as dev dependencies, for example npm install --save-dev @types/lodash. For an installed package without types the compiler reports TS7016 instead, and suggests the @types package or a declare module line. For Node.js's own modules install @types/node and, from TypeScript 6.0, add node to types in tsconfig.json.
My @/components import works in the editor but tsc fails. Why?
The alias must be listed under compilerOptions.paths in the tsconfig.json that tsc actually uses. In TypeScript 6.0 baseUrl is deprecated and in 7.0 it is gone, so write paths entries relative to the tsconfig file, starting with ./src/ for code in src. Remember that paths does not change the emitted JavaScript, so your bundler or runtime needs the same alias.
How do I fix TS2307 for .css, .svg or .png imports?
TypeScript does not know these file types. In a Vite project add vite/client to compilerOptions.types or a vite-env.d.ts file with a reference to vite/client, which declares asset and CSS module imports. Elsewhere add a .d.ts file with a declare module line whose name is a wildcard pattern for the file type, for example for all files ending in .svg.
Why does nodenext want me to write .js on an import of a .ts file?
Under node16 and nodenext, TypeScript follows Node.js, which does not support extensionless relative paths in import. You write the path the compiled file will have, ./utils.js, and TypeScript finds ./utils.ts behind it. The compiler suggests the right path in TS2835.
Sources
- TypeScript source — tsc/internal/diagnostics/diagnosticMessages.json (github.com). Checked .
- TypeScript — TSConfig Reference (typescriptlang.org). Checked .
- TypeScript Handbook — Modules: Reference (typescriptlang.org). Checked .
- TypeScript Handbook — Consumption (declaration files) (typescriptlang.org). Checked .
- TypeScript blog — Announcing TypeScript 6.0 (devblogs.microsoft.com). Checked .
- TypeScript blog — Announcing TypeScript 7.0 (devblogs.microsoft.com). Checked .
- Vite — Features (TypeScript, Client Types) (vite.dev). Checked .
- Vite source — packages/vite/client.d.ts (github.com). Checked .
Put your project online today
Connect GitHub or drop a folder. No card is asked for.
Deploy your Node appRelated
- Error: Cannot find module (MODULE_NOT_FOUND) in Node.js: how to fix itError: Cannot find module 'x' with code MODULE_NOT_FOUND, or ERR_MODULE_NOT_FOUND with import, means Node.js looked for a package or file and it was not there. On a server the usual causes are a package missing from package.json, a devDependency the install skipped, a path whose case differs from the file, a start command pointing at a file that was never built, or an ES module import without its .js extension.
- Module not found: Can't resolve './components/Header' in webpack and Next.js: how to fix itModule not found: Can't resolve './components/Header' means the bundler followed an import and found no file or package there. The usual causes: the file name's case differs from the import (fine on macOS and Windows, fatal on a Linux build server), a wrong relative path or a missing extension, a package that is not in package.json, an @/ alias the bundler does not know, and, for Can't resolve 'fs', Node-only code pulled into the browser bundle.
- SyntaxError: Cannot use import statement outside a module: how to fix it in Node.jsSyntaxError: Cannot use import statement outside a module means Node.js loaded a file as CommonJS and found an ES module import in it. The reverse, ReferenceError: require is not defined in ES module scope, means it loaded the file as an ES module and found require. Node.js decides by the file extension and the type field in package.json: set "type": "module", rename the file to .mjs or .cjs, or compile TypeScript with module nodenext and start the built file.
- sh: vite: not found (also next, tsc) in npm run build: how to fix itsh: 1: vite: not found, sh: next: not found or tsc: not found during npm run build (exit code 127) means the shell found no such command in node_modules/.bin. Usually the install skipped devDependencies because NODE_ENV=production or --omit=dev, the install never ran in that folder, or the tool was only installed globally on your laptop. Install it into the project and make the build's install include dev dependencies.
- npm failed with return code 1 (npm ERR! code 1): how to fix the buildnpm failed with return code 1, or npm install exited with 1, is npm ERR! code 1 in newer npm: it only says that a script npm ran exited with code 1. The real error is printed above it: usually a compile or type error, a lockfile out of sync with package.json, or a dependency whose install script failed. Find the first error line above code 1, fix that, and the build passes.
- Node.js hosting in 2026: how to deploy an Express appA Node.js server in production needs one long-running process that listens on the port it is given, restarts when it crashes and exits cleanly on SIGTERM. On Livops you push a repository with package.json and it runs as a web app for 39 credits a month (39,000 so'm). Here is what Node needs, and how a VPS, Render, Railway, Fly.io, DigitalOcean and Heroku compare.