{"id":11056,"library":"http-parser-js","title":"Pure JavaScript HTTP Parser","description":"http-parser-js is a JavaScript implementation of an HTTP protocol parser, designed to replace Node.js's internal C-based `http_parser.c` module. Its primary differentiator is its flexibility and tolerance for malformed or non-standard HTTP data, making it suitable for interacting with legacy services that do not strictly adhere to HTTP parsing rules, unlike Node.js's default stricter parser. The package is currently at version 0.5.10. It does not follow a strict release cadence but updates as needed for compatibility or bug fixes. While originally created to mitigate V8's C++ function call overhead, its current main benefit lies in its error tolerance. It can be used both as a monkey-patch replacement for Node.js's internal parser or as a standalone component for parsing raw HTTP buffers.","status":"active","version":"0.5.10","language":"javascript","source_language":"en","source_url":"git://github.com/creationix/http-parser-js","tags":["javascript","http","typescript"],"install":[{"cmd":"npm install http-parser-js","lang":"bash","label":"npm"},{"cmd":"yarn add http-parser-js","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-parser-js","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"While Node.js uses CommonJS, this project also ships TypeScript types and can be imported via ESM syntax in environments supporting it, although its primary use case involves CommonJS `require` for monkey-patching.","wrong":"const HTTPParser = require('http-parser-js').HTTPParser;","symbol":"HTTPParser","correct":"import { HTTPParser } from 'http-parser-js';"},{"note":"This specific line must be executed *before* `require('http')` to effectively replace Node.js's internal parser.","wrong":"require('http-parser-js'); // This only imports the module, not patches.","symbol":"monkey-patching entry","correct":"process.binding('http_parser').HTTPParser = require('http-parser-js').HTTPParser;"}],"quickstart":{"code":"import { HTTPParser } from 'http-parser-js';\nimport http from 'http';\n\n// Monkey patch before you require http for the first time.\n// Note: This specific usage pattern primarily targets CommonJS environments in Node.js.\n// For ESM, you might need a different approach or a build step to ensure `require` behavior.\nif (typeof process.binding === 'function' && process.binding('http_parser')) {\n  process.binding('http_parser').HTTPParser = HTTPParser; // Assuming HTTPParser is already imported\n  console.log('http-parser-js successfully monkey-patched into Node.js runtime.');\n}\n\n// Now, `http` module will use http-parser-js\nconst server = http.createServer((req, res) => {\n  console.log(`Received request for: ${req.url}`);\n  res.writeHead(200, { 'Content-Type': 'text/plain' });\n  res.end('Hello from http-parser-js powered server!\\n');\n});\n\nserver.listen(3000, () => {\n  console.log('Server running on port 3000');\n});\n\n// Example of making a request to demonstrate it's working\n// setTimeout(() => {\n//   http.get('http://localhost:3000/', (res) => {\n//     let data = '';\n//     res.on('data', (chunk) => { data += chunk; });\n//     res.on('end', () => { console.log('Client received:', data); server.close(); });\n//   }).on('error', (err) => {\n//     console.error('Client error:', err.message);\n//     server.close();\n//   });\n// }, 1000);\n","lang":"typescript","description":"This example demonstrates how to monkey-patch Node.js's internal HTTP parser with `http-parser-js` before initializing the native `http` module, then starts a basic HTTP server."},"warnings":[{"fix":"Run Node.js v12.x with `node --http-parser=legacy file.js` to opt into the older, patchable binding.","message":"Node.js v12.x renamed its internal HTTP parser and did not expose it for direct monkey-patching by default. Attempting to patch without a specific flag will fail.","severity":"breaking","affected_versions":"=12.x"},{"fix":"Ensure the `process.binding('http_parser').HTTPParser = require('http-parser-js').HTTPParser;` line is at the absolute top of your application's entry point, before any `require('http')` statements.","message":"Monkey-patching must occur *before* the native Node.js `http` module is `require`d for the first time. Loading `http` prematurely will result in it using the default C++ parser.","severity":"gotcha","affected_versions":">=0.5.0"},{"fix":"Refer to the `standalone-example.js` in the package's GitHub repository for guidance on how to use the raw `HTTPParser` class for manual parsing.","message":"The API for standalone usage (not monkey-patching) is described as 'somewhat awkward' due to its adherence to Node.js internal compatibility. Directly using `HTTPParser` requires careful handling of raw buffers and state management.","severity":"gotcha","affected_versions":">=0.5.0"},{"fix":"Only use this package if you specifically need its tolerant parsing behavior or have strong reasons to avoid C++ bindings; otherwise, rely on Node.js's default `http` module behavior.","message":"This library is designed to replace Node.js's internal C parser for specific use cases (tolerance, pure JS). For most standard applications, Node.js's default parser offers superior performance due to its C++ implementation.","severity":"gotcha","affected_versions":">=0.5.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Execute your Node.js application with the flag: `node --http-parser=legacy your-app.js`","cause":"Attempting to monkey-patch `http-parser-js` on Node.js v12.x without enabling the legacy HTTP parser.","error":"TypeError: process.binding(...).HTTPParser is not a constructor"},{"fix":"Install the package: `npm install http-parser-js` or `yarn add http-parser-js`","cause":"The package `http-parser-js` has not been installed as a dependency.","error":"Error: Cannot find module 'http-parser-js'"},{"fix":"Move the `process.binding('http_parser').HTTPParser = require('http-parser-js').HTTPParser;` line to the very top of your application's main entry file, ensuring it runs before any other code that might implicitly or explicitly load the `http` module.","cause":"The monkey-patching code was executed *after* the `http` module was first `require`d in the application lifecycle.","error":"The Node.js `http` module is still using the default parser, despite patching attempts."}],"ecosystem":"npm"}