Property 'code' does not exist on type 'Error'

The real issue is that the Node.js definition file isn't exporting a proper Error definition. It uses the following for Error (and doesn't export this):

interface Error {
    stack?: string;
}

The actual definition it exports is in the NodeJS namespace:

export interface ErrnoException extends Error {
    errno?: number;
    code?: string;
    path?: string;
    syscall?: string;
    stack?: string;
}

So the following typecast will work:

.catch((error: NodeJS.ErrnoException) => {
    console.log(error);
    console.log(error.code);
})

This seems like a flaw in Node's definition, since it doesn't line up with what an object from new Error() actually contains. TypeScript will enforce the interface Error definition.


You have to cast a type to the error param from catch i.e.

.catch((error:any) => {
    console.log(error);
    console.log(error.code);
});

or you can access the code property directly in this manner

.catch((error) => {
    console.log(error);
    console.log(error['code']);
});