node-http2
The `node-http2` package (version 4.0.1) provides a pure JavaScript implementation of the HTTP/2 client and server protocols, aiming for API compatibility with Node.js's standard HTTPS module. Originally forked from `molnarg/node-http2`, this specific `kaazing/node-http2` version was released around 2017. Its primary differentiating features were its full JavaScript implementation and inclusion of server push capabilities prior to widespread native HTTP/2 support in Node.js. However, since Node.js v8.0.0 (released May 2017) included experimental built-in HTTP/2 support, and especially with its stabilization in Node.js v10.0.0, this package has been superseded. As such, it is no longer actively maintained and should generally not be used in new projects.
Common errors
-
Error: Cannot find module 'http2'
cause Attempting to `require('http2')` when the current Node.js version does not have the native HTTP/2 module enabled, or an incorrect path is used. Alternatively, if `node-http2` was intended, it might not be installed or resolved correctly.fixIf intending to use Node.js's native module, ensure your Node.js version is >= 8.0.0 (for experimental) or >= 10.0.0 (for stable) and use `require('node:http2')`. If specifically targeting *this* `node-http2` package, ensure `npm install node-http2` has been run and use `require('node-http2')`. -
TypeError: http2.createServer is not a function
cause This error often occurs when `require('http2')` resolves to the native Node.js `http2` module, which exposes `createSecureServer` and `createServer` directly but with potentially different arguments or behavior than `node-http2`, or vice-versa.fixExplicitly decide which module you intend to use. For the native Node.js module: `const http2 = require('node:http2'); http2.createSecureServer(...)`. For this legacy `node-http2` package: `const http2 = require('node-http2'); http2.createServer(...)`. Pay attention to whether `createSecureServer` (native) or `createServer` (this package) is expected, and the required options. -
SSL_CTX_set_alpn_protos failed
cause HTTP/2 relies heavily on TLS and ALPN for secure connections. This error can indicate issues with TLS setup, incorrect certificate/key files, or an incompatibility with the Node.js crypto module or OpenSSL version, especially given the age of this library.fixDouble-check your `key` and `cert` file paths and permissions. Ensure they are valid TLS certificates. If using an older Node.js, update to a newer version (preferably one with native http2 support) to get modern TLS features. If using `node-http2`, ensure Node.js >= 5.0 for ALPN.
Warnings
- breaking This package (`node-http2` by Kaazing) is completely superseded by the native `http2` module included in Node.js core since v8.0.0 (stable in v10.0.0). Using this package in modern Node.js environments will cause dependency conflicts, unexpected behavior, and prevent access to newer HTTP/2 features and performance optimizations. Do not use this package for new development.
- gotcha The package is no longer maintained. This means it will not receive updates for security vulnerabilities, bug fixes, or compatibility with newer Node.js versions or HTTP/2 specification updates.
- gotcha ALPN (Application-Layer Protocol Negotiation) support within this `node-http2` package is explicitly stated to only work for Node.js versions >= 5.0. Earlier Node.js versions may encounter issues establishing HTTP/2 connections.
- deprecated The original `molnarg/node-http2` repository (from which this package was forked) explicitly marked itself as deprecated, advising users to switch to the Node.js built-in `http2` module. This deprecation applies to all forks, including this `kaazing/node-http2` package.
Install
-
npm install node-http2 -
yarn add node-http2 -
pnpm add node-http2
Imports
- createServer
const http2 = require('http2'); // This often resolves to the native moduleimport { createServer } from 'node-http2'; - get
import { get } from 'node:http2'; // This is the native module API, not this packageconst http2 = require('node-http2'); http2.get(url, callback); - Http2Server
import { Http2Server } from 'http2'; // Will likely fail or resolve to nativeconst { Http2Server } = require('node-http2');
Quickstart
const fs = require('fs');
const http2 = require('node-http2'); // Explicitly import this package
// Ensure you have localhost.key and localhost.crt in an 'example' directory
// For testing, you can generate self-signed certificates:
// openssl genrsa -out example/localhost.key 2048
// openssl req -new -x509 -key example/localhost.key -out example/localhost.crt -days 365 -subj "/CN=localhost"
const options = {
key: fs.readFileSync('./example/localhost.key'),
cert: fs.readFileSync('./example/localhost.crt')
};
http2.createServer(options, function(request, response) {
response.end('Hello world from node-http2!');
}).listen(8080, () => {
console.log('node-http2 server listening on port 8080 (HTTPS/2)');
console.log('Try: curl --http2 -k https://localhost:8080/');
});