Maaz Zindani

Loading the index…

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

A namespace-style import cannot be called or constructed (import * as express)

ErrorA namespace-style import cannot be called or constructed, and will cause a failure at runtime

Short answer

A namespace import (import * as x) is by spec an object, never a function. With esModuleInterop on, TypeScript refuses to let you call one. Switch to a default import (import express from 'express') and make sure esModuleInterop is true. That form works in Node.js at runtime.

  • Applies to TypeScript 5.9, Node.js 22, Express 4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find the import that is called as a function later in the file, and read your esModuleInterop value.

    grep -rn "import \* as" src
    npx tsc --showConfig | grep -i esModuleInterop
  2. 2Match what you see, then jump to its fix

    1. 1The message points at a line like import * as express from 'express' followed by express()A CommonJS function is imported with import * asFix
    2. 2After switching to the default import, tsc says the module has no default export or can only be imported with a flagesModuleInterop is off, so a default import is rejectedFix

What does the error look like?

  • A namespace-style import cannot be called or constructed, and will cause a failure at runtime

Cause 1 of 2

A CommonJS function is imported with import * as

1Check

A CommonJS package that exports a function or class is a callable value, not a namespace. Under the ES module rules a namespace import can only be an object, so TypeScript now blocks the call.

grep -rn "import \* as express" src

2Fix

Use a default import for the callable export.

// before
import * as express from 'express';
// after
import express from 'express';
const app = express();

3Verify

Compile and start the app.

npx tsc
node dist/index.js

Confirmed by typescriptlang.org

Did this fix it?

Cause 2 of 2

esModuleInterop is off, so a default import is rejected

1Check

esModuleInterop is true when module is node16, nodenext or preserve, and false otherwise. Check the value from the first step.

npx tsc --showConfig | grep -i -E 'esModuleInterop|"module"'

2Fix

Turn esModuleInterop on. It also enables allowSyntheticDefaultImports, which the default import needs.

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

3Verify

Type-check, then run the compiled app.

npx tsc --noEmit
node dist/index.js

Confirmed by typescriptlang.org

Did this fix it?

How do I stop it coming back?

  • Keep esModuleInterop true in every new tsconfig.
  • Use default imports for packages that export a function, and named imports for the rest.
  • Do not mix import styles for the same package in one project.

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

Back end

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' flag

TS1259 means you wrote a default import for a CommonJS module that TypeScript does not treat as having one.

3 causes, each with a fix

Next step

Want this handled for good?

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