{"id":17269,"library":"http2-client","title":"Transparent HTTP/2 and HTTP/1.1 Client for Node.js","description":"http2-client is a Node.js library designed to provide a transparent, drop-in replacement for Node's native `http` and `https` modules. It allows applications to make requests that automatically negotiate between HTTP/1.1 and HTTP/2 protocols using ALPN, without requiring changes to the existing `http.request` or `http.get` interfaces. This is particularly useful for integrating HTTP/2 capabilities into older modules or applications that are tightly coupled to the HTTP/1.1 API. The library manages connection pooling for both protocols, ensuring efficient resource utilization. The current stable version is 1.3.5. Based on the provided release history (v1.3.2, v1.3.3), the package appears to have an infrequent release cadence, suggesting a maintenance rather than actively developed status. Key differentiators include its adherence to the standard Node.js `http` module API and automatic protocol detection.","status":"maintenance","version":"1.3.5","language":"javascript","source_language":"en","source_url":"https://github.com/hisco/http2-client","tags":["javascript","http2","http2 client","request","compatible","client","compatibility"],"install":[{"cmd":"npm install http2-client","lang":"bash","label":"npm"},{"cmd":"yarn add http2-client","lang":"bash","label":"yarn"},{"cmd":"pnpm add http2-client","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily uses CommonJS syntax. Direct ES module imports are not officially supported or documented, potentially leading to errors.","wrong":"import { request } from 'http2-client';","symbol":"request","correct":"const { request } = require('http2-client');"},{"note":"Similar to `request`, `get` is a named export. Attempting a default import or ESM named import will fail.","wrong":"import get from 'http2-client';","symbol":"get","correct":"const { get } = require('http2-client');"},{"note":"HttpRequestManager is a named export for creating custom request managers with specific defaults and separate caches.","wrong":"new http2Client.HttpRequestManager();","symbol":"HttpRequestManager","correct":"const { HttpRequestManager } = require('http2-client');"}],"quickstart":{"code":"const { request } = require('http2-client');\n\nconst h1Target = 'http://www.example.com/';\nconst h2Target = 'https://www.google.com/'; // Using google as example.com might not support h2\n\nconsole.log('Making HTTP/1.1 request...');\nconst req1 = request(h1Target, (res) => {\n    console.log(`\\nUrl : ${h1Target}\\nStatus : ${res.statusCode}\\nHttpVersion : ${res.httpVersion}`);\n    res.on('data', () => {}); // Consume response data\n    res.on('end', () => console.log('HTTP/1.1 request ended.'));\n});\nreq1.on('error', (e) => console.error(`HTTP/1.1 request error: ${e.message}`));\nreq1.end();\n\nconsole.log('\\nMaking HTTP/2 request...');\nconst req2 = request(h2Target, (res) => {\n    console.log(`\\nUrl : ${h2Target}\\nStatus : ${res.statusCode}\\nHttpVersion : ${res.httpVersion}`);\n    res.on('data', () => {}); // Consume response data\n    res.on('end', () => console.log('HTTP/2 request ended.'));\n});\nreq2.on('error', (e) => console.error(`HTTP/2 request error: ${e.message}`));\nreq2.end();\n","lang":"javascript","description":"This quickstart demonstrates how to use `http2-client`'s `request` function to make transparent requests to both HTTP/1.1 and HTTP/2 servers using the same API as Node's native `http` module. It prints the URL, status code, and HTTP version for each response."},"warnings":[{"fix":"Ensure your project is configured for CommonJS, or use dynamic `import()` if absolutely necessary within an ESM context, though this is not officially supported by the library.","message":"The package is primarily designed for CommonJS environments. While Node.js itself supports ESM, `http2-client`'s documentation and examples exclusively use `require()`. Attempting direct ES module `import` syntax may lead to runtime errors or require bundler configuration.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"If HTTP/2 server push is a requirement, consider using Node.js's native `http2` module directly or a different HTTP client library that offers full HTTP/2 feature parity.","message":"The README states 'All Http2 features are available except push.' Server push is a core HTTP/2 feature that this client explicitly does not support. Applications relying on HTTP/2 server push will need an alternative solution.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Evaluate modern alternatives like `undici` or Node.js's built-in `fetch` API, which offer robust HTTP/2 support, especially for newer projects, if active development and broader feature sets are crucial.","message":"The description claims 'it's the only http2/https compatible API for clients.' This claim is likely outdated given the evolution of Node.js's native HTTP/2 support, the introduction of `fetch` in Node.js, and other modern HTTP client libraries. Relying on this package as the *only* option might miss more current and actively maintained alternatives.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure your Node.js version (typically >=8.8.1 for `http2` module availability) supports HTTP/2 if you intend to use it. Monitor `res.httpVersion` in your callbacks to confirm the protocol used.","message":"The library automatically falls back to HTTP/1.1 if the Node.js version doesn't support HTTP/2. While convenient, this might mask underlying environment issues or unexpected performance characteristics if HTTP/2 was expected but not achieved.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"First, ensure `npm install http2-client` was successful. If using a bundler, verify CommonJS modules are correctly handled. If running in an ES module context, consider if `require()` is appropriate or if a different library designed for ESM is needed.","cause":"This error typically indicates that `http2-client` was installed or imported incorrectly, or that the Node.js environment has issues resolving the module.","error":"TypeError: Cannot destructure property 'request' of 'require(...)' as it is undefined."},{"fix":"Remove explicit `protocol` options if you want the library to automatically handle protocol detection based on the URL. Ensure your Node.js version supports ALPN for HTTPS/2 negotiation (typically Node.js 8.8.1+).","cause":"While `http2-client` supports HTTPS, this error might occur if an incorrect `protocol` option is explicitly set in the request options, overriding the automatic detection, or if an older Node.js version with limited HTTPS/ALPN capabilities is used.","error":"Error: Protocol \"https:\" not supported. Expected \"http:\""}],"ecosystem":"npm","meta_description":null}