Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

TS1259: module can only be default-imported with esModuleInterop

Errorerror TS1259: Module '"./node_modules/@types/express/index"' can only be default-imported using the 'esModuleInterop' flag

Short 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.

  • Applies to TypeScript 5, Node.js 22, ts-node 10, express 4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 --showConfig
  2. 2Match what you see, then jump to its fix

    1. 1The error names the module and the esModuleInterop flag, and the import is import express from 'express'esModuleInterop is offFix
    2. 2tsconfig.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. 3tsc is clean but ts-node src/index.ts reports TS1259ts-node reads another tsconfig than you expectFix

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.json

2Fix

Turn the option on. It also turns on allowSyntheticDefaultImports.

{
  "compilerOptions": {
    "esModuleInterop": true
  }
}

3Verify

Run the compiler again; TS1259 should be gone.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

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 --showConfig

2Fix

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.json

3Verify

The compile should print no TS1259 line.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

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_PROJECT

2Fix

Point ts-node at the tsconfig that holds the option.

npx ts-node -P ./tsconfig.json src/index.ts

3Verify

The script should start without a compile error.

npx ts-node -P ./tsconfig.json src/index.ts

Confirmed by typestrong.org

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.