Async resource type must be a string
Production Risk
Low — caught immediately at construction; use descriptive string type names.
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.
- 1Passing a number or object as the type argument to new AsyncResource()
- 2Passing undefined or null as the type
Triggered when AsyncResource validates the type argument in its constructor.
const { AsyncResource } = require('async_hooks');
new AsyncResource(123); // type must be a stringexpected 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
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.
const { AsyncResource } = require('async_hooks');
new AsyncResource(123); // type must be a string // this triggers ERR_ASYNC_TYPEtry {
// 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
}
}// Validate inputs before calling the operation
function safe_err_async_type(...args) {
// validate args here
return performOperation(...args)
}✕ Pass non-string values as the AsyncResource type
The type is used for identification and must be a string.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev