Skip to content

[Refactor] async_hooks loaded via require() in ESM logging module — incompatible at runtime #772

Description

@RUKAYAT-CODER

Overview

src/lib/logging/index.ts lines 19-28 attempt to dynamically require('async_hooks') inside an ES module. require() is not available in native ESM contexts (which Next.js 15 uses by default with type: module in package.json). This causes a ReferenceError: require is not defined at startup in certain environments, disabling the async-context-based request correlation.

Specifications

Features:

  • Async context tracking uses ESM-compatible APIs
  • Logger correctly associates log entries with the current request context

Tasks:

  • Replace require('async_hooks') with a top-level import { AsyncLocalStorage } from 'node:async_hooks'
  • Wrap the import in a dynamic await import('node:async_hooks') if conditional loading is needed
  • Test that async context is correctly propagated through await chains

Impacted Files:

  • src/lib/logging/index.ts

Acceptance Criteria

  • No require() call exists in src/lib/logging/index.ts
  • Logger starts without errors in the Next.js runtime
  • Request IDs are correctly associated across async boundaries

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions