Maaz Zindani

Loading the index…

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

TypeError: Class constructor cannot be invoked without 'new' in compiled TypeScript

ErrorTypeError: Class constructor X cannot be invoked without 'new'

Short answer

Node.js throws this when a native class is called like a plain function. In TypeScript projects it usually means one of two things: your own code calls a class without new, or you compile to ES5 so a subclass calls its ES2015 parent class as a function. Add new, or set target to ES2015 or newer.

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

Fix it fast

  1. 1Find out which cause you have

    Read the stack trace to find the line that calls the class, then check which target your compiler uses.

    npx tsc --showConfig | grep -E '"(target|module)"'
  2. 2Match what you see, then jump to its fix

    1. 1The trace points at a line such as X() or a call through a variable that holds a classThe code calls a class like a functionFix
    2. 2The error appears from a class you wrote that extends a class from a library (or from a built-in), and tsconfig target is ES5 or unsetES5 target: a downleveled subclass extends a native classFix

What does the error look like?

  • TypeError: Class constructor X cannot be invoked without 'new'
  • TypeError: Class constructors cannot be invoked without 'new'

Cause 1 of 2

The code calls a class like a function

1Check

A class constructor is always a constructor. Unlike an old-style function, it cannot run as an ordinary call, so MyClass() throws even if it looked fine in the TypeScript source.

grep -rn "MyClass(" src | grep -v "new MyClass("

2Fix

Create the instance with new. If a factory was meant, wrap the class in a function that calls new.

// wrong
const a = MyClass();
// right
const a = new MyClass();

3Verify

Run the compiled file again.

node dist/index.js

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 2

ES5 target: a downleveled subclass extends a native class

1Check

target controls which features are downleveled, and its default is ES5 unless module is node20 or nodenext. At ES5 your own classes are turned into plain functions, and a plain function cannot construct a native ES2015 class by calling it. Check the target in the first-step command.

npx tsc --showConfig | grep -n target

2Fix

Set target to ES2015 or newer, so your classes stay real classes. For Node.js 22 a modern target is safe; pick one that matches your Node.js version.

{
  "compilerOptions": {
    "target": "ES2022"
  }
}

3Verify

Rebuild from a clean output folder and run it.

rm -rf dist && npx tsc
node dist/index.js

Confirmed by typescriptlang.org

Did this fix it?

How do I stop it coming back?

  • Set target explicitly in tsconfig.json; do not rely on the default.
  • Use a target that matches the oldest Node.js version you deploy to.
  • Delete the old dist folder when you change target, so no stale ES5 files remain.

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

cdk synth says --app is required

--app is required either in command-line, in cdk.json or in ~/.cdk.json

The CDK CLI needs a command that runs your app and finds it in three places: the --app option, cdk.json in the project folder, or cdk.json in your home folder.

3 causes, each with a fix

Back end

Mocha or Node cannot find ts-node/register

Error: Cannot find module 'ts-node/register'

Node resolves ts-node/register from your project's node_modules, so the package must be installed there, next to typescript.

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