Registry / http-networking / node-fetch-har

node-fetch-har

JSON →
library1.0.1jsnpmunverified

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-har
INSTALL
IMPORT
SIG · NODE-FETCH-HAR
N
node-fetch-har
http-networkingjavascriptv1.0.1
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.

withHar
import { withHar } from 'node-fetch-har';
const { withHar } = require('node-fetch-har');
Primary function to wrap a fetch instance. The package is ESM-first, commonJS requires can lead to issues without proper bundler configuration or for certain Node.js versions.
createHarLog
import { createHarLog } from 'node-fetch-har';
const createHarLog = require('node-fetch-har').createHarLog;
Used to create a HAR object to aggregate multiple entries. Best used with ESM imports.
response.harEntry
response.harEntry
HAR entry is accessed as a property on the `Response` object returned by the wrapped fetch function. No explicit import is needed for this property.

Demonstrates wrapping `node-fetch` with `withHar`, creating a HAR log with `createHarLog`, and collecting multiple request entries into it concurrently.

import { withHar, createHarLog } from 'node-fetch-har'; import nodeFetch from 'node-fetch'; async function recordHarLogs() { const har = createHarLog(); const fetch = withHar(nodeFetch, { har }); try { console.log('Making concurrent fetch requests...'); await Promise.all([ fetch('https://httpstat.us/200'), fetch('https://httpstat.us/500'), fetch('https://httpstat.us/200?sleep=100') ]); console.log('Requests completed. Generated HAR log:'); console.log(JSON.stringify(har, null, 2)); // Example: Accessing harEntry on a single response const singleResponse = await fetch('https://httpstat.us/200'); console.log('\nSingle response HAR entry:'); console.log(JSON.stringify(singleResponse.harEntry, null, 2)); } catch (error) { console.error('An error occurred during fetch operations:', error); } } recordHarLogs();
Debug
Known issues
gotchaHAR files can contain highly sensitive information like cookies, authentication tokens, and even passwords within request bodies. Exercise extreme caution when exposing or sharing captured HAR data, ensuring it is only accessible to authorized superusers or in secure development/staging environments.
fix
Implement strict access controls and sanitization processes for any generated HAR data before storage or transmission.
affects: >=0.5.0
gotchaThis library is designed exclusively for server-side Node.js environments. It relies on Node.js built-in modules and is not intended for use in browsers. Attempting to use it directly in a browser context will result in errors.
fix
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.
affects: >=0.5.0
breakingPrior to v1.0.1, socket event listeners (`lookup`, `connect`, `secureConnect`) were not always explicitly removed and could stick around on reused sockets, leading to potential resource leaks, especially when using connection pooling agents like `agentkeepalive`.
fix
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`.
affects: <1.0.1
Errors
Common errors & fixes
ReferenceError: process is not defined
Attempting to run `node-fetch-har` code in a browser environment without proper conditional import or bundler configuration.
fix
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.
TypeError: withHar is not a function (or similar for createHarLog)
Incorrect import statement, often using CommonJS `require()` syntax with an ESM-first package or incorrect named import destructuring.
fix
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.
Error: Cannot find module 'node-fetch'
`node-fetch` is a peer dependency and must be explicitly installed alongside `node-fetch-har`.
fix
Install the `node-fetch` package: `npm install node-fetch` or `yarn add node-fetch`.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
node-fetchrequiredPeer dependency providing the underlying Fetch API implementation to be instrumented.
Agent activity
12 hits · last 30 days
node
10
OpenAI (training)
2
Resources
node-fetch-har — npm install node-fetch-har · libregistry