TypeError: Class constructor cannot be invoked without 'new' in compiled TypeScript
TypeError: 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.
Fix it fast
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)"'2Match what you see, then jump to its fix
- 1
The 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
The 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
- 1
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.jsConfirmed by developer.mozilla.org
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 target2Fix
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.jsConfirmed by typescriptlang.org
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:
- 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 '@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 fixBack end
Angular build fails with TS2707 or "has no exported member" after installing a library
Module '"@angular/core"' has no exported member
These errors mean a library was compiled by a newer Angular than your app uses.
3 causes, each with a fixBack 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 fixBack 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
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 fixBack end
create-next-app keeps creating a TypeScript project
Would you like to use TypeScript? No / Yes
create-next-app now makes a TypeScript project by default.
2 causes, each with a fix