Registry / networking / node-fetch-h2

node-fetch-h2

JSON →
library2.3.1-0jsnpmunverified

http2-aware fork of node-fetch 2.x, providing a window.fetch-compatible API in Node.js using native http2 where possible. Currently at v2.3.1-0 (pre-release), based on node-fetch 2.x with added http2 support. Key differentiators: seamless http2 upgrade for requests, falls back to http1.1 if not supported, maintains the same API surface as node-fetch. Lower adoption and less mature than node-fetch itself, but useful for services that require http2 connections.

npm install node-fetch-h2
INSTALL
IMPORT
SIG · NODE-FETCH-H2
N
node-fetch-h2
networkingjavascriptv2.3.1-0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

default
import fetch from 'node-fetch-h2'
const fetch = require('node-fetch-h2')
ESM default import; CJS require works but ESM is preferred for tree-shaking. The module has no named exports.
Request
import { Request } from 'node-fetch-h2'
const Request = require('node-fetch-h2').Request
Named export for the Request class. CJS destructured require works, but avoid using it as a constructor without new.
Headers
import { Headers } from 'node-fetch-h2'
const { Headers } = require('node-fetch-h2')
Same pattern as node-fetch; Headers class is exported as a named export.

Basic GET request with JSON response using node-fetch-h2 with http2 support.

import fetch from 'node-fetch-h2'; async function main() { try { const response = await fetch('https://httpbin.org/get', { method: 'GET', headers: { 'Accept': 'application/json' } }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); console.log(data); } catch (error) { console.error('Error:', error.message); } } main();
Debug
Known issues
deprecatednode-fetch-h2 is a pre-release and may have breaking changes between minor versions.
fix
Pin to exact version (e.g., 2.3.1-0) and test upgrades thoroughly.
affects: >=2.3.0
gotchahttp2 support depends on the server; if the server does not support http2, the library falls back to http1.1. This is automatic but may cause confusion if http2 is expected.
fix
Ensure the upstream server supports http2 or handle http1.1 responses.
affects: *
gotchaThe library uses a custom http2 client which may have different performance characteristics than native http2.
fix
Benchmark with your specific workload if performance is critical.
affects: *
breakingIn v2.3.0, the default export changed from a CommonJS require to an ES module. Ensure your environment supports ES modules.
fix
Use dynamic import() or set type: 'module' in package.json.
affects: >=2.3.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module not supported
Using require() on an ES Module export in v2.3.0+
fix
Use dynamic import: const fetch = await import('node-fetch-h2')
TypeError: fetch is not a function
Importing the module without default export or using wrong import syntax
fix
Ensure you use import fetch from 'node-fetch-h2' (ESM) or const fetch = require('node-fetch-h2') (CJS, if supported)
Error: connect ECONNREFUSED 127.0.0.1:80
No http2 support on the target server, causing fallback to http1.1 which fails to connect
fix
Check that the server is running and accessible on http1.1 or verify http2 support.
Upgrade
Version history
2.3.1-0latest on npm
Audit
Dependencies
http2-clientrequiredProvides http2 client session management for connections to http2-enabled servers
Agent activity
11 hits · last 30 days
node
10
Resources
node-fetch-h2 — npm install node-fetch-h2 · libregistry