TypeError: something.default is not a constructor in a NestJS app
TypeError: xxx_1.default is not a constructorShort answer
TypeScript turns import X from 'pkg' into require('pkg').default when esModuleInterop is off, and a CommonJS package has no default, so new X() fails. Turn esModuleInterop on. With it on, import * as X can no longer be called as a constructor.
Fix it fast
1Find out which cause you have
Look at how the package is imported and what the compiler option is set to.
grep -n "esModuleInterop\|allowSyntheticDefaultImports" tsconfig*.json grep -rnE "^import .* from 'ioredis'|^import \* as" src | head2Match what you see, then jump to its fix
- 1
The error names pkg_1.default, as in redis_1.default is not a constructorDefault import of a CommonJS package with esModuleInterop offFix - 2
The error says X is not a constructor right after esModuleInterop was enabled, with import * as XNamespace import used as a constructor with esModuleInterop onFix
- 1
What does the error look like?
TypeError: xxx_1.default is not a constructorException has occurred: TypeError: xxx.default is not a constructorA package that works with require() fails when imported with import in TypeScript
Cause 1 of 2
Default import of a CommonJS package with esModuleInterop off
1Check
With esModuleInterop unset, TypeScript treats a default import as require('pkg').default. A CommonJS module without a default property gives undefined.
grep -n "esModuleInterop" tsconfig.json2Fix
Set esModuleInterop to true in tsconfig.json. The compiler then emits a helper that wraps modules without __esModule so the default is the module itself.
{
"compilerOptions": {
"esModuleInterop": true
}
}3Verify
Rebuild and start the app; the constructor call works.
rm -rf dist
npm run build && node dist/main.jsConfirmed by typescriptlang.org
Cause 2 of 2
Namespace import used as a constructor with esModuleInterop on
1Check
A namespace import must be an object under the ES module rules, so with esModuleInterop it is no longer callable or constructable.
grep -rn "import \* as" src2Fix
Change the namespace import to a default import for that package.
// before
import * as Client from 'some-cjs-client'
const c = new Client()
// after
import Client from 'some-cjs-client'
const c = new Client()3Verify
Build again and exercise the code path that creates the object.
npm run buildConfirmed by typescriptlang.org
How do I stop it coming back?
- Keep esModuleInterop the same across your tsconfig files and in the jest config that compiles tests.
- Prefer the import style shown in the package's own README for 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.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
NestJS app will not start: Cannot find module '/app/dist/main'
Cannot find module 'dist/main'
Node cannot find dist/main.js because the build did not put it there.
4 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
TypeORM warns that createConnection is deprecated
'createConnection' is deprecated.
TypeORM replaced Connection with DataSource.
2 causes, each with a fixBack end
TypeORM stops with Duplicate migrations on run or generate
Error: Duplicate migrations: User1655123435373
TypeORM collects every migration class that your migrations glob matches and refuses to continue when two of them have the same class name.
2 causes, each with a fixBack end
TypeORM cannot find an entity, or the metadata for a relation
Entity metadata for Role#users was not found.
TypeORM only knows the entities listed in the entities option of the DataSource that is running.
2 causes, each with a fix