TypeScript: Option 'module' must be set to 'NodeNext'
moduleResolution and module are not independent: the Node16 and NodeNext resolution modes read package.json exports and the type field, and only the matching module setting emits code that agrees with them. Copying a moduleResolution line from a bundler config into a Node project is the usual way to land here.
Quick fix
Read the commands before running them. Anything that restarts a service, deletes data or changes permissions should be tried on a non-production system first.
// Node project, ESM or CommonJS decided by package.json "type"
{
"compilerOptions": {
"module": "nodenext",
"moduleResolution": "nodenext",
"target": "es2022"
}
}
// Bundler project (Vite, webpack, esbuild): the other valid pair
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "bundler"
}
}
# See what the compiler ended up with after extends and defaults
npx tsc --showConfig | grep -Ei '"(module|moduleResolution|target)"'
# NodeNext also makes relative imports need the .js extension in ESM
# import { x } from "./util.js"; // even though the file is util.ts
How to diagnose TypeScript errors
TypeScript errors are the compiler describing a mismatch between what you declared and what you did. Nearly all of them fall into assignability (this shape is not that shape), missing declarations (a JavaScript package with no types), or strict null checks (a value can be undefined on a path you did not handle). Casting with as or any silences the message without fixing the underlying mismatch, which usually reappears at runtime.
If the quick fix above does not resolve it, work through these steps. They apply to this whole class of error, not just to this one message, which is usually what saves the time.
- Read the error from the innermost "Types of property X are incompatible" line outward. That nested line names the actual mismatch.
- For missing declarations, try
npm i -D @types/<pkg>first; if none exists, write a minimal.d.tsrather than reaching forany. - Narrow rather than assert. A type guard, an
incheck, or an early return preserves safety whereasdiscards it. - Use
tsc --noEmit --prettyto check the whole project; editors sometimes use a different TypeScript version than the build. - Check
strict,skipLibCheckandmoduleResolutionin tsconfig when errors appear only in CI or only in the editor.
Tools worth reaching for
tsc --noEmitts-node / tsx@types/* packagestypescript-eslinteditor TS version selector
Authoritative references
Primary documentation for this error, worth reading before applying any fix in production.
Related TypeScript errors
- TypeScript: 'error' is of type 'unknown' (TS18046)Since TypeScript 4.4, useUnknownInCatchVariables (part of strict) types the catch binding as…
- TypeScript: Argument of type X is not assignable to parameter of type YA function argument's type doesn't match the parameter's declared type. Common when passing…
- TypeScript: Cannot find module './x.js' or its corresponding type declarationsWith module set to node16 or nodenext, TypeScript resolves imports exactly as Node does at…
- TypeScript: Cannot find name 'process'The Node global types are not loaded. Either @types/node is missing, or the…
- TypeScript: Cannot redeclare block-scoped variable 'name'A file with no import or export is treated as a script, not a module, so its top level…
- TypeScript: Cannot use JSX unless the '--jsx' flag is providedA .tsx file was compiled with JSX support off. Either the option is missing, or the editor is…
- TypeScript: Could not find a declaration file for moduleA JS library has no TypeScript type declarations. The library has no built-in types and no…
- TypeScript: Module can only be default-imported using the 'esModuleInterop' flagThe package is CommonJS and sets module.exports directly, so it has no ES default export to…
Browse other categories
- HTTP 494xx client errors, 5xx server errors, redirects, headers and protocol problems.
- JavaScript 42npm resolution, async pitfalls, hydration, memory limits and runtime type…
- Database 41Connections, deadlocks, constraints, replication and memory limits.
- AI 35Rate limits, context windows, GPU memory and model-serving failures.
- Network 35Refused connections, timeouts, resets, MTU problems and port exhaustion.
- Python 35Imports, virtual environments, encoding, concurrency and dependency conflicts.
- Kubernetes 34CrashLoopBackOff, ImagePullBackOff, OOMKilled, RBAC, scheduling and storage.
- Docker 27Daemon connectivity, disk space, image pulls, ports and architecture mismatches.
- System 26Disk space, systemd units, file descriptors, OOM killer and scheduled jobs.
- Cloud 25IAM permissions, quotas, service limits and credential failures.
- Security 25JWT validation, CSRF, OAuth grants, SELinux, SSH host keys and CSP.
- TLS 24Untrusted authorities, expiry, hostname mismatch, chains and cipher negotiation.
Something missing or wrong?
This entry is maintained by hand. If the fix is out of date, incomplete, or you have a better one, email a correction and it will be reviewed.