express-msgpack is an Express middleware that provides transparent support for MessagePack (msgpack) content types, enabling developers to handle `application/msgpack` requests and responses using standard Express `req.body` and `res.json` patterns. It automatically decodes incoming MessagePack payloads into JavaScript objects and encodes outgoing JavaScript objects into MessagePack format when clients request it via the `Accept` header. The current stable version is 6.0.0, which mandates Node.js 20.12.0 or higher. The package is actively maintained with releases frequently aligned with Node.js version updates and new features such as `allowUnacceptableResponse` and `limit` options. A key differentiator is its seamless integration with existing Express API patterns, which reduces boilerplate, and the flexibility to utilize custom MessagePack encoder/decoder libraries.
npm install express-msgpackVerified import paths — ran on the pinned version, not inferred.
Demonstrates setting up a basic Express server with `express-msgpack` middleware. It shows how to initialize the middleware for transparent MessagePack handling and includes commented-out examples for configuring custom encoder/decoder functions and other options like `limit` and `allowUnacceptableResponse`. It handles both GET and POST requests, highlighting how `res.json` and `req.body` adapt to MessagePack content negotiation.
Upgrade your Node.js runtime to version 20.12.0 or higher. If unable to upgrade Node.js, you must remain on an older `express-msgpack` version compatible with your current Node.js runtime (e.g., v5.x for Node.js v18).
For ES Module projects, use `import msgpack from 'express-msgpack';`. For CommonJS projects, use `const msgpack = require('express-msgpack').default;` (since v4.1.0). Always verify your Node.js version meets the requirements of the specific major version of `express-msgpack` you are using.Update your CommonJS import statements according to your `express-msgpack` version: use `const { default: msgpack } = require("express-msgpack");` for versions 2.x up to 4.0.0, or `const msgpack = require("express-msgpack").default;` for versions 4.1.0 and later.Ensure `@msgpack/msgpack` is successfully installed (it's installed by default). If you intend to use a different MessagePack library, explicitly pass its `decoder` and `encoder` functions: `app.use(msgpack({ decoder: yourDecoder, encoder: yourEncoder }))`.To allow `express-msgpack` to send MessagePack responses even if `application/msgpack` is not explicitly in the client's `Accept` header, set the `allowUnacceptableResponse` option to `true`: `app.use(msgpack({ allowUnacceptableResponse: true }))`.For CJS, try `const msgpack = require('express-msgpack').default;` (for v4.1.0+). For older CJS versions (v2.x-v4.0.0), use `const { default: msgpack } = require('express-msgpack');`. In TypeScript, ensure your `tsconfig.json` `moduleResolution` and `allowSyntheticDefaultImports` are configured appropriately, or stick to explicit `require().default` for CJS.Configure your project as an ES module by adding `"type": "module"` to your `package.json` file, or switch to the correct CommonJS `require` syntax: `const msgpack = require('express-msgpack').default;` (since v4.1.0).Increase the `limit` option when initializing the middleware, e.g., `app.use(msgpack({ limit: '5mb' }))`. Ensure the limit string format is supported by the `bytes` package.If you are using a custom `encoder` function, verify that it correctly serializes your JavaScript object into a valid MessagePack `Buffer` or `Uint8Array`.