node-fetch-har is a Node.js library that provides a wrapper around the popular `node-fetch` package, enabling the automatic generation of HTTP Archive (HAR) logs for server-side HTTP requests. This functionality is crucial for debugging, performance analysis, and capturing detailed network activity on the backend, analogous to a browser's network tab. Currently at version 1.0.1, the library maintains a stable release cycle, having solidified its API after a series of release candidates for its 1.0.0 major version. A key differentiator is its seamless integration with existing `node-fetch` usage, offering options to attach HAR entries directly to response objects, use an `onHarEntry` callback, or collect multiple entries into a single HAR log via `createHarLog`. It supports custom `node-fetch` agents, tracks various request timings (DNS, connect, SSL, send), and handles redirects and request body data, making it a comprehensive tool for server-side request introspection. Unlike browser-based HAR generation, this library is strictly for server environments due to its reliance on Node.js built-in modules and the sensitive nature of data it can capture.
npm install node-fetch-harVerified import paths — ran on the pinned version, not inferred.
Demonstrates wrapping `node-fetch` with `withHar`, creating a HAR log with `createHarLog`, and collecting multiple request entries into it concurrently.
Implement strict access controls and sanitization processes for any generated HAR data before storage or transmission.
Conditionally import and use `node-fetch-har` only in server-side code paths. For isomorphic applications, use `process.browser` checks with bundler configuration (e.g., Webpack) to tree-shake out server-specific code from browser bundles.
Upgrade to `node-fetch-har@1.0.1` or newer to ensure socket event listeners are correctly attached with `once` and explicitly removed on request `finish`.
Wrap `node-fetch-har` imports and usage within `if (!process.browser)` blocks and configure your bundler (e.g., Webpack, Rollup) to process `process.browser` for browser builds.
Ensure you are using `import { withHar, createHarLog } from 'node-fetch-har';` for modern Node.js environments and bundlers. If forced to use CommonJS, consider dynamic `import()` or verify transpilation settings.Install the `node-fetch` package: `npm install node-fetch` or `yarn add node-fetch`.