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 runtimeShort 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.
Fix it fast
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 esModuleInterop2Match what you see, then jump to its fix
- 1
The message points at a line like import * as express from 'express' followed by express()A CommonJS function is imported with import * asFix - 2
After 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
- 1
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" src2Fix
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.jsConfirmed by typescriptlang.org
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.jsConfirmed by typescriptlang.org
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:
- 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
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 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