ERR_ASYNC_TYPE
Node.jsERRORCriticalAsyncHIGH confidence

Async resource type must be a string

Production Risk

Low — caught immediately at construction; use descriptive string type names.

What this means

Thrown when the type argument passed to AsyncResource() is not a string. The type identifies the category of async resource for tracking purposes in async_hooks and must be a non-empty string.

Why it happens
  1. 1Passing a number or object as the type argument to new AsyncResource()
  2. 2Passing undefined or null as the type
How to reproduce

Triggered when AsyncResource validates the type argument in its constructor.

trigger — this will error
trigger — this will error
const { AsyncResource } = require('async_hooks');
new AsyncResource(123); // type must be a string

expected output

TypeError [ERR_ASYNC_TYPE]: Invalid name for async "type": 123

Fix

Pass a descriptive string as the resource type

WHEN When creating AsyncResource instances

Pass a descriptive string as the resource type
const { AsyncResource } = require('async_hooks');
const resource = new AsyncResource('MyDatabaseQuery');
resource.runInAsyncScope(() => {
  // tracked as 'MyDatabaseQuery' in async_hooks
});

Why this works

A string type label satisfies the type check and provides meaningful context in async traces.

Code examples
Triggerjs
const { AsyncResource } = require('async_hooks');
new AsyncResource(123); // type must be a string  // this triggers ERR_ASYNC_TYPE
Handle in try/catchjs
try {
  // operation that may throw ERR_ASYNC_TYPE
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_ASYNC_TYPE') {
    console.error('ERR_ASYNC_TYPE:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_async_type(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Pass non-string values as the AsyncResource type

The type is used for identification and must be a string.

Sources
Official documentation ↗

Node.js Error Codes Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Node.js errors