TS1259: module can only be default-imported with esModuleInterop
error TS1259: Module '"./node_modules/@types/express/index"' can only be default-imported using the 'esModuleInterop' flagShort answer
TS1259 means you wrote a default import for a CommonJS module that TypeScript does not treat as having one. Set esModuleInterop to true in tsconfig.json. If the error stays, the compiler is ignoring your tsconfig because files are passed on the command line.
Fix it fast
1Find out which cause you have
Print the settings TypeScript really uses. If esModuleInterop is missing or false, start with the first cause.
npx tsc --showConfig2Match what you see, then jump to its fix
- 1
The error names the module and the esModuleInterop flag, and the import is import express from 'express'esModuleInterop is offFix - 2
tsconfig.json has esModuleInterop: true and TS1259 still appears when you run tsc index.tsThe option is set, but tsc is run on file namesFix - 3
tsc is clean but ts-node src/index.ts reports TS1259ts-node reads another tsconfig than you expectFix
- 1
What does the error look like?
error TS1259: Module '"./node_modules/@types/express/index"' can only be default-imported using the 'esModuleInterop' flag
Cause 1 of 3
esModuleInterop is off
1Check
The TypeScript reference explains that without the flag, a default import compiles to a read of .default from the required module, which a CommonJS module usually does not have. esModuleInterop adds helpers that wrap it.
grep -n "esModuleInterop" tsconfig.json2Fix
Turn the option on. It also turns on allowSyntheticDefaultImports.
{
"compilerOptions": {
"esModuleInterop": true
}
}3Verify
Run the compiler again; TS1259 should be gone.
npx tsc --noEmitConfirmed by typescriptlang.org
Cause 2 of 3
The option is set, but tsc is run on file names
1Check
The TypeScript docs say that when input files are given on the command line, tsconfig.json files are ignored.
npx tsc --showConfig2Fix
Run tsc with no file names so it compiles the closest tsconfig.json, or point it to one with --project.
npx tsc
npx tsc --project tsconfig.json3Verify
The compile should print no TS1259 line.
npx tsc --noEmitConfirmed by typescriptlang.org
Cause 3 of 3
ts-node reads another tsconfig than you expect
1Check
ts-node takes its configuration from a tsconfig.json it finds, or from the path you pass with -P (capital P, unlike tsc).
printenv TS_NODE_PROJECT2Fix
Point ts-node at the tsconfig that holds the option.
npx ts-node -P ./tsconfig.json src/index.ts3Verify
The script should start without a compile error.
npx ts-node -P ./tsconfig.json src/index.tsConfirmed by typestrong.org
How do I stop it coming back?
- Start every new project from tsc --init and keep esModuleInterop in the committed tsconfig.json.
- Run npx tsc --noEmit in CI so a missing option fails the build early.
- Keep one tsconfig for the app and pass it explicitly to every tool that compiles TypeScript.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
Related fixes
Back end
TypeScript says req.user does not exist on the Express Request type
Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'
The Request type from Express has no user field until you declare one.
2 causes, each with a fixBack end
MikroORM refuses to use the global EntityManager
ValidationError: Using global EntityManager instance methods for context specific actions is disallowed
MikroORM no longer allows the global identity map.
3 causes, each with a fixBack end
TypeScript cannot find req.cookies in a NestJS controller
error TS2339: Property 'cookies' does not exist on type
TypeScript says 'cookies' is missing because the Request type in your file is not Express's, or because cookie support is not installed.
3 causes, each with a fixBack end
A namespace-style import cannot be called or constructed (import * as express)
A namespace-style import cannot be called or constructed, and will cause a failure at runtime
A namespace import (import * as x) is by spec an object, never a function.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
TypeScript cannot find '@angular/platform-browser' in an Angular project
Cannot find module '@angular/platform-browser' or its corresponding type declarations.
TS2307 means TypeScript could not turn the import into a file.
3 causes, each with a fix