Skip to main content
isNativeError - node__util--types.d.ts - Node documentation
function isNativeError

Usage in Deno

```typescript import { isNativeError } from "node:node__util--types.d.ts"; ```
isNativeError(object: unknown): object is Error
Returns `true` if the value was returned by the constructor of a [built-in `Error` type](https://tc39.es/ecma262/#sec-error-objects). ```js console.log(util.types.isNativeError(new Error())); // true console.log(util.types.isNativeError(new TypeError())); // true console.log(util.types.isNativeError(new RangeError())); // true ``` Subclasses of the native error types are also native errors: ```js class MyError extends Error {} console.log(util.types.isNativeError(new MyError())); // true ``` A value being `instanceof` a native error class is not equivalent to `isNativeError()` returning `true` for that value. `isNativeError()` returns `true` for errors which come from a different [realm](https://tc39.es/ecma262/#realm) while `instanceof Error` returns `false` for these errors: ```js import vm from 'node:vm'; const context = vm.createContext({}); const myError = vm.runInContext('new Error()', context); console.log(util.types.isNativeError(myError)); // true console.log(myError instanceof Error); // false ``` Conversely, `isNativeError()` returns `false` for all objects which were not returned by the constructor of a native error. That includes values which are `instanceof` native errors: ```js const myError = { __proto__: Error.prototype }; console.log(util.types.isNativeError(myError)); // false console.log(myError instanceof Error); // true ```

Parameters

object: unknown

Return Type

object is Error