Maaz Zindani

Loading the index…

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

TypeError: something.default is not a constructor in a NestJS app

ErrorTypeError: xxx_1.default is not a constructor

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

  • Applies to NestJS 10, NestJS 11, TypeScript 5.x, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

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

    1. 1The error names pkg_1.default, as in redis_1.default is not a constructorDefault import of a CommonJS package with esModuleInterop offFix
    2. 2The error says X is not a constructor right after esModuleInterop was enabled, with import * as XNamespace import used as a constructor with esModuleInterop onFix

What does the error look like?

  • TypeError: xxx_1.default is not a constructor
  • Exception has occurred: TypeError: xxx.default is not a constructor
  • A 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.json

2Fix

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

Confirmed by typescriptlang.org

Did this fix it?

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" src

2Fix

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 build

Confirmed by typescriptlang.org

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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