Registry / http-networking / milliparsec

milliparsec

JSON →
library5.1.1jsnpmunverified

Milliparsec is a minimalistic and performant HTTP body parser for modern Node.js environments, currently at version 5.1.1. It provides middleware for parsing JSON, raw, URL-encoded, and multipart form data, making it compatible with frameworks like tinyhttp and Express. The library boasts a tiny package size (under 8KB) and zero dependencies, setting it apart from more heavy-weight alternatives like `body-parser` and `formidable`, offering significantly faster parsing speeds (approximately 15% faster than `body-parser` and 4x faster than `formidable`). It receives active development, with several minor and patch releases in quick succession, indicating a steady release cadence for bug fixes and feature enhancements, such as the recently added `type` and `reviver` options for JSON parsing in v5.1.0.

npm install milliparsec
INSTALL
IMPORT
SIG · MILLIPARSEC
M
milliparsec
http-networkingjavascriptv5.1.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.

json
import { json } from 'milliparsec'
const json = require('milliparsec')
Milliparsec is an ESM-first package and is primarily designed for `import` statements. While recent Node.js versions (20.19.0+ and 22.12.0+) offer improved `require()` interoperability with ESM, using `import` is the idiomatic and most reliable approach.
urlencoded
import { urlencoded } from 'milliparsec'
const urlencoded = require('milliparsec')
Similar to `json`, `urlencoded` is a named export. Ensure you use named imports and understand the ESM-first nature of the library.
raw
import { raw } from 'milliparsec'
const raw = require('milliparsec')
For handling raw buffer data, the `raw` middleware is available as a named export. It's crucial to use modern ESM import syntax.

Demonstrates setting up an HTTP server with Node.js and `milliparsec` to parse a JSON request body using the `json` middleware. It includes basic error handling and type annotations.

import { createServer } from 'node:http'; import { json } from 'milliparsec'; // Define a basic type for the request body for TypeScript clarity interface ReqWithBody extends Request { body?: object; } const server = createServer(async (req: ReqWithBody, res) => { if (req.method === 'POST' && req.url === '/parse-json') { await json()(req, res, (err) => { if (err) { console.error('Error parsing JSON:', err); res.statusCode = 400; return res.end(JSON.stringify({ error: err.message })); } }); res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify({ message: 'JSON parsed successfully', body: req.body })); } else { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Send a POST request to /parse-json with a JSON body.'); } }); const PORT = process.env.PORT ?? 3000; server.listen(PORT, () => { console.log(`Server listening on http://localhost:${PORT}`); }); // Example of how to test with curl: // curl -X POST -H "Content-Type: application/json" -d '{"hello": "world"}' http://localhost:3000/parse-json
Debug
Known issues
breakingNode.js engine requirements were increased. Previous versions supported Node.js 12.20+, but from v5.0.2 onwards, it requires `node: >=18.13 || >=19.20 || >=20`. Users on older Node.js versions must upgrade to use v5.x.
fix
Upgrade your Node.js runtime to version 18.13+, 19.20+, or 20+.
affects: >=5.0.2
breakingVersion 5.0.0 introduced a significant internal rewrite of the primary `p` function, shifting from strings to buffers for parsing. This change primarily affects users implementing custom parsers, as the underlying data handling mechanism has changed.
fix
Review any custom parsers you have implemented with `milliparsec` to ensure compatibility with buffer-based data handling, adjusting logic as needed.
affects: >=5.0.0
breakingMilliparsec is built for modern Node.js and is predominantly an ESM-first module. While Node.js has improved CJS-ESM interoperability, relying on `require()` might lead to issues or require specific configurations, especially in older Node.js environments.
fix
Migrate your codebase to use ES Module `import` syntax (`import { json } from 'milliparsec'`) for better compatibility and future-proofing.
affects: >=3.0.0
gotchaWhen using `milliparsec` middleware, it must be `await`ed, as the parsing process is asynchronous. Failing to `await` the middleware call will result in `req.body` being `undefined` or not fully populated.
fix
Always use `await` when invoking a `milliparsec` middleware function (e.g., `await json()(req, res, callback)`).
affects: >=3.0.0
gotchaFrom v5.0.0, `milliparsec` introduced request limits. Large payloads exceeding these limits will be rejected, potentially leading to 'PayloadTooLargeError' errors.
fix
If encountering payload size errors, check the middleware options (e.g., `limit` option for JSON/text parsers) and increase them if necessary, or ensure client-side requests conform to the server's limits.
affects: >=5.0.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported
Attempting to use `require()` to import `milliparsec` in a CommonJS module context.
fix
Convert your module to ES Modules by setting `"type": "module"` in `package.json` or by renaming files to `.mjs`, and use `import { json } from 'milliparsec'`.
TypeError: Cannot read properties of undefined (reading 'body')
The `milliparsec` middleware was not awaited, or the callback was not handled correctly, resulting in `req.body` not being populated.
fix
Ensure the middleware call is `await`ed and the callback for error handling is provided and processed: `await json()(req, res, (err) => { /* handle err */ });`
SyntaxError: Invalid JSON
The client sent a request with a `Content-Type: application/json` header, but the body content was not valid JSON.
fix
Ensure the client sends correctly formatted JSON. On the server side, implement robust error handling within the middleware callback to gracefully respond to invalid input.
PayloadTooLargeError: request entity too large
The incoming request body exceeded the configured size limit for the `milliparsec` parser.
fix
Configure the `limit` option when initializing the parser (e.g., `json({ limit: '10mb' })`) to allow larger payloads, or instruct clients to send smaller requests.
Upgrade
Version history
5.1.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources