new Error(message, { cause }) is rejected by TypeScript
Object literal may only specify known properties, and 'cause' does not exist in typeShort answer
The cause option belongs to the ES2022 error typings. If tsc rejects new Error(msg, { cause }) or err.cause, your lib setting is older than ES2022. Set target to ES2022 or newer, or add es2022 to an explicit lib list, and restart the type checker or watcher.
Fix it fast
1Find out which cause you have
See which target and lib the compiler is really using, including values inherited from a base config.
npx tsc --showConfig | grep -i -E '"(target|lib)"'2Match what you see, then jump to its fix
- 1
lib is set (for example ["es2020"]) and the error shows on the Error constructorAn explicit lib list stops below ES2022Fix - 2
No lib is set and target is ES5, ES2015 or similar, or unsettarget is old, so the default lib is oldFix - 3
Reading err.cause in a catch block reports that cause does not existThe declarations in es2022.error add cause to every error typeFix
- 1
What does the error look like?
Object literal may only specify known properties, and 'cause' does not exist in typeExpected 0-1 arguments, but got 2.Property 'cause' does not exist on type 'Error'.
Cause 1 of 3
An explicit lib list stops below ES2022
1Check
The ErrorOptions type with cause lives in the es2022.error library file. A lib list that does not include it, directly or through a newer entry, hides it.
grep -n '"lib"' tsconfig.json2Fix
Add ES2022 or newer to lib, keeping the other entries you need.
{
"compilerOptions": {
"lib": ["ES2022"]
}
}3Verify
Type-check the file that uses cause.
npx tsc --noEmitConfirmed by typescriptlang.org
Cause 2 of 3
target is old, so the default lib is old
1Check
Changing target also changes the default value of lib, and TypeScript includes APIs matching the target. The default target is ES5 unless module is node20 or nodenext.
npx tsc --showConfig | grep -i target2Fix
Raise the target to ES2022 or newer, or keep your target and set lib as in the first cause.
{
"compilerOptions": {
"target": "ES2022"
}
}3Verify
Type-check again. With ts-node or nodemon, restart the process so it reads the new config.
npx tsc --noEmitConfirmed by typescriptlang.org
Cause 3 of 3
The declarations in es2022.error add cause to every error type
1Check
The same library file declares cause on the Error interface, typed unknown, and gives the Error constructors an optional options argument.
grep -rn "cause" node_modules/typescript/lib/lib.es2022.error.d.ts2Fix
After lib or target includes ES2022, read the value as unknown and narrow it before use.
try {
run();
} catch (e) {
if (e instanceof Error && e.cause instanceof Error) {
console.error(e.cause.message);
}
}3Verify
Type-check the catch block.
npx tsc --noEmitConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Pick target and lib from the oldest Node.js version you deploy to, and write both down in tsconfig.
- Restart ts-node, nodemon or the editor's TypeScript server after changing tsconfig.
- Wrap errors with new Error(msg, { cause }) so the original stack is kept.
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
TypeError: Class constructor cannot be invoked without 'new' in compiled TypeScript
TypeError: Class constructors cannot be invoked without 'new'
Node.js throws this when a native class is called like a plain function.
2 causes, each with a fix