Registry / observability / has-async-hooks

has-async-hooks

JSON →
library1.0.0jsnpmunverified

This package, `has-async-hooks`, provides a lightweight utility function to programmatically determine if the `async_hooks` API is available in the current Node.js runtime environment. Currently at version 1.0.0, it is a stable, single-purpose library. Its primary differentiator is its simplicity and explicit focus on feature detection for `async_hooks`, which became stable in Node.js 8.0.0. This utility is particularly useful for conditional code execution or polyfilling in environments where compatibility with various Node.js versions (especially older ones) is a concern. The library has a very stable release cadence, with its initial 1.0.0 release being its sole version to date, indicating its 'fire and forget' nature once the feature detection logic is established.

npm install has-async-hooks
INSTALL
IMPORT
SIG · HAS-ASYNC-HOOKS
H
has-async-hooks
observabilityjavascriptv1.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

hasAsyncHooks
import hasAsyncHooks from 'has-async-hooks'
import { hasAsyncHooks } from 'has-async-hooks'
The package exports a default function. For ES Modules, use a default import.
hasAsyncHooks (CommonJS)
const hasAsyncHooks = require('has-async-hooks')
This is the standard CommonJS import pattern. The module exports the function directly.

Demonstrates how to import and use the has-async-hooks function to check for async_hooks API availability, providing conditional logic for feature-dependent code.

const hasAsyncHooks = require('has-async-hooks'); if (hasAsyncHooks()) { console.log('async_hooks API is available in this Node.js version.'); // Example usage: You might conditionally load async_hooks dependent code const async_hooks = require('async_hooks'); const asyncId = async_hooks.executionAsyncId(); console.log(`Current async ID: ${asyncId}`); } else { console.log('async_hooks API is NOT available in this Node.js version.'); console.log('This typically means you are running an older Node.js version (pre-8.x).'); } // To demonstrate: This script will output 'true' on Node.js >= 8, 'false' on older versions. // You can test by running with different Node.js versions.
Debug
Known issues
gotchaThis utility only detects the presence of the `async_hooks` API; it does not polyfill or provide an alternative implementation for environments where it's absent. Developers must implement their own fallback logic.
fix
Implement conditional loading or alternative logic based on the `hasAsyncHooks()` return value in your application code.
affects: *
gotchaIf `hasAsyncHooks()` returns `false`, it indicates that the current Node.js runtime is likely older than version 8.0.0, as `async_hooks` became a stable API in Node.js 8.x. The module does not explicitly check the Node.js version, only the API availability.
fix
Be aware of Node.js version compatibility; if your application requires `async_hooks`, ensure a minimum Node.js 8.x environment.
affects: *
Errors
Common errors & fixes
TypeError: hasAsyncHooks is not a function
Incorrect import syntax or attempting to access a named export that does not exist. The package exports a single default function.
fix
Ensure you are importing the default function correctly: `const hasAsyncHooks = require('has-async-hooks')` for CommonJS or `import hasAsyncHooks from 'has-async-hooks'` for ES Modules.
ERR_REQUIRE_ESM
Attempting to use `require()` in an ES Module context without proper configuration for CommonJS interop.
fix
If your project is an ES Module (e.g., `"type": "module"` in package.json), use `import hasAsyncHooks from 'has-async-hooks'` instead of `require('has-async-hooks')`.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
2
Resources
has-async-hooks — npm install has-async-hooks · libregistry