# msgpack-lite

> Fast Pure JavaScript MessagePack Encoder and Decoder

Latest version **0.2.2** (published 2026-09-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install msgpack-lite
pnpm add msgpack-lite
yarn add msgpack-lite
bun add msgpack-lite
```

Provides the command `msgpack`.

## Health

**Score 68/100 (B)** — status: active.

Positive: has types package; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.2.2 |
| Published | 2026-09-02 |
| First published | 2015-07-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/msgpack-lite) |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 117.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 1036 |
| Author | @kawanet |
| Maintainers | kawanet |
| Keywords | arraybuffer, buffer, fluentd, messagepack, msgpack, serialize, stream, typedarray, uint8array |

## Links

- npm: https://www.npmjs.com/package/msgpack-lite
- Repository: https://github.com/kawanet/msgpack-lite
- Issues: https://github.com/kawanet/msgpack-lite/issues
- npm.io page: https://npm.io/package/msgpack-lite

## Dependencies (4)

- [ieee754](https://npm.io/package/ieee754.md) ^1.2.1
- [isarray](https://npm.io/package/isarray.md) ^2.0.5
- [event-lite](https://npm.io/package/event-lite.md) ^1.0.0
- [int64-buffer](https://npm.io/package/int64-buffer.md) ^1.1.1

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 0.2.2 (latest) — 2026-09-02
- 0.2.1 — 2026-08-27
- 0.2.0 — 2026-08-26
- 0.1.27 — 2026-08-26
- 0.1.26 — 2016-10-14
- 0.1.20 — 2016-04-22
- 0.1.19 — 2016-04-20
- 0.1.18 — 2016-04-13
- 0.1.17 — 2016-02-26
- 0.1.16 — 2016-02-16
- 0.1.15 — 2016-01-04
- 0.1.14 — 2015-12-21
- 0.1.13 — 2015-09-04
- 0.1.11 — 2015-08-23
- 0.1.9 — 2015-08-22
- … 4 more at https://npm.io/package/msgpack-lite/versions

## README

# msgpack-lite

[![Node.js CI](https://github.com/kawanet/msgpack-lite/workflows/Node.js%20CI/badge.svg?branch=main)](https://github.com/kawanet/msgpack-lite/actions/)
[![npm version](https://img.shields.io/npm/v/msgpack-lite)](https://www.npmjs.com/package/msgpack-lite)
[![gzip size](https://img.badgesize.io/https://cdn.jsdelivr.net/npm/msgpack-lite/dist/msgpack.min.js?compression=gzip)](https://cdn.jsdelivr.net/npm/msgpack-lite/dist/msgpack.min.js)

Fast Pure JavaScript MessagePack Encoder and Decoder

Online demo: [http://kawanet.github.io/msgpack-lite/](http://kawanet.github.io/msgpack-lite/)

### Encoding and Decoding MessagePack

```js
const msgpack = require("msgpack-lite");

// encode from JS Object to MessagePack (Buffer)
const buffer = msgpack.encode({"foo": "bar"});

// decode from MessagePack (Buffer) to JS Object
const data = msgpack.decode(buffer); // => {"foo": "bar"}

// if encode/decode receives an invalid argument an error is thrown
```

### Writing to MessagePack Stream

```js
const fs = require("fs");
const msgpack = require("msgpack-lite");

const writeStream = fs.createWriteStream("test.msp");
const encodeStream = msgpack.createEncodeStream();
encodeStream.pipe(writeStream);

// send multiple objects to stream
encodeStream.write({foo: "bar"});
encodeStream.write({baz: "qux"});

// call this once you're done writing to the stream.
encodeStream.end();
```

### Reading from MessagePack Stream

```js
const fs = require("fs");
const msgpack = require("msgpack-lite");

const readStream = fs.createReadStream("test.msp");
const decodeStream = msgpack.createDecodeStream();

// show multiple objects decoded from stream
readStream.pipe(decodeStream).on("data", console.warn);
```

### Decoding MessagePack Bytes Array

```js
const msgpack = require("msgpack-lite");

// decode() accepts Buffer instance per default
msgpack.decode(Buffer.from([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]));

// decode() also accepts Array instance
msgpack.decode([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]);

// decode() accepts raw Uint8Array instance as well
msgpack.decode(new Uint8Array([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]));
```

### Command Line Interface

A CLI tool bin/msgpack converts data stream from JSON to MessagePack and vice versa.

```sh
$ echo '{"foo": "bar"}' | ./bin/msgpack -Jm | od -tx1
0000000    81  a3  66  6f  6f  a3  62  61  72

$ echo '{"foo": "bar"}' | ./bin/msgpack -Jm | ./bin/msgpack -Mj
{"foo":"bar"}
```

### Installation

```sh
$ npm install --save msgpack-lite
```

### Tests

Run tests on node.js:

```sh
$ make test
```

### Browser Build

Browser version [msgpack.min.js](https://cdn.jsdelivr.net/npm/msgpack-lite/dist/msgpack.min.js) is also available. 50KB minified, 14KB gziped.

```html
<!--[if lte IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.1.10/es5-shim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script>
<![endif]-->
<script src="https://cdn.jsdelivr.net/npm/msgpack-lite/dist/msgpack.min.js"></script>
<script>
// encode from JS Object to MessagePack (Uint8Array)
var buffer = msgpack.encode({foo: "bar"});

// decode from MessagePack (Uint8Array) to JS Object
var array = new Uint8Array([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]);
var data = msgpack.decode(array);
</script>
```

### MessagePack Mapping Table

The following table shows how JavaScript objects (value) will be mapped to 
[MessagePack formats](https://github.com/msgpack/msgpack/blob/master/spec.md)
and vice versa.

Source Value|MessagePack Format|Value Decoded
----|----|----
null, undefined|nil format family|null
Boolean (true, false)|bool format family|Boolean (true, false)
Number (32bit int)|int format family|Number (int or double)
Number (64bit double)|float format family|Number (double)
String|str format family|String
Buffer|bin format family|Buffer
Array|array format family|Array
Map|map format family|Map (if `usemap=true`)
Object (plain object)|map format family|Object (or Map if `usemap=true`)
Object (see below)|ext format family|Object (see below)

Note that both `null` and `undefined` are mapped to nil `0xC1` type.
This means `undefined` value will be *upgraded* to `null` in other words.

### Extension Types

The MessagePack specification allows 128 application-specific extension types. 
The library uses the following types to make round-trip conversion possible
for JavaScript native objects.

Type|Object|Type|Object
----|----|----|----
0x00||0x10|
0x01|EvalError|0x11|Int8Array
0x02|RangeError|0x12|Uint8Array
0x03|ReferenceError|0x13|Int16Array
0x04|SyntaxError|0x14|Uint16Array
0x05|TypeError|0x15|Int32Array
0x06|URIError|0x16|Uint32Array
0x07||0x17|Float32Array
0x08||0x18|Float64Array
0x09||0x19|Uint8ClampedArray
0x0A|RegExp|0x1A|ArrayBuffer
0x0B|Boolean|0x1B|Buffer
0x0C|String|0x1C|
0x0D|Date|0x1D|DataView
0x0E|Error|0x1E|
0x0F|Number|0x1F|

Other extension types are mapped to built-in ExtBuffer object.

### Custom Extension Types (Codecs)

Register a custom extension type number to serialize/deserialize your own class instances.

```js
const msgpack = require("msgpack-lite");

const codec = msgpack.createCodec();
codec.addExtPacker(0x3F, MyVector, myVectorPacker);
codec.addExtUnpacker(0x3F, myVectorUnpacker);

const data = new MyVector(1, 2);
const encoded = msgpack.encode(data, {codec: codec});
const decoded = msgpack.decode(encoded, {codec: codec});

function MyVector(x, y) {
  this.x = x;
  this.y = y;
}

function myVectorPacker(vector) {
  const array = [vector.x, vector.y];
  return msgpack.encode(array); // return Buffer serialized
}

function myVectorUnpacker(buffer) {
  const array = msgpack.decode(buffer);
  return new MyVector(array[0], array[1]); // return Object deserialized
}
```

The first argument of `addExtPacker` and `addExtUnpacker` should be an integer within the range of 0 and 127 (0x0 and 0x7F). `myVectorPacker` is a function that accepts an instance of `MyVector`, and should return a buffer representing that instance. `myVectorUnpacker` is the opposite: it accepts a buffer and should return an instance of `MyVector`.

If you pass an array of functions to `addExtPacker` or `addExtUnpacker`, the value to be encoded/decoded will pass through each one in order. This allows you to do things like this:

```js
codec.addExtPacker(0x00, Date, [Number, msgpack.encode]);
```

You can also pass the `codec` option to `msgpack.Decoder(options)`, `msgpack.Encoder(options)`, `msgpack.createEncodeStream(options)`, and `msgpack.createDecodeStream(options)`.

If you wish to modify the default built-in codec, you can access it at `msgpack.codec.preset`.

### Custom Codec Options

`msgpack.createCodec()` function accepts some options.

It does NOT have the preset extension types defined when no options given.

```js
const codec = msgpack.createCodec();
```

`preset`: It has the preset extension types described above.

```js
const codec = msgpack.createCodec({preset: true});
```

`safe`: It runs a validation of the value before writing it into buffer. This is the default behavior for some old browsers which do not support `ArrayBuffer` object.

```js
const codec = msgpack.createCodec({safe: true});
```

`useraw`: It uses `raw` formats instead of `bin` and `str`.

```js
const codec = msgpack.createCodec({useraw: true});
```

`int64`: It decodes msgpack's `int64`/`uint64` formats with [int64-buffer](https://github.com/kawanet/int64-buffer) object.

```js
const codec = msgpack.createCodec({int64: true});
```

`binarraybuffer`: It ties msgpack's `bin` format with `ArrayBuffer` object, instead of `Buffer` object.

```js
const codec = msgpack.createCodec({binarraybuffer: true, preset: true});
```

`uint8array`: It returns Uint8Array object when encoding, instead of `Buffer` object.

```js
const codec = msgpack.createCodec({uint8array: true});
```

`usemap`: Uses the global JavaScript Map type, if available, to unpack
MessagePack map elements.

```js
const codec = msgpack.createCodec({usemap: true});
```

### Compatibility Mode

The [compatibility mode](https://github.com/kawanet/msgpack-lite/issues/22) respects for [msgpack's old spec](https://github.com/msgpack/msgpack/blob/master/spec-old.md). Set `true` to `useraw`.

```js
// default mode handles both str and bin formats individually
msgpack.encode("Aa"); // => <Buffer a2 41 61> (str format)
msgpack.encode(Buffer.from([0x41, 0x61])); // => <Buffer c4 02 41 61> (bin format)

msgpack.decode(Buffer.from([0xa2, 0x41, 0x61])); // => 'Aa' (String)
msgpack.decode(Buffer.from([0xc4, 0x02, 0x41, 0x61])); // => <Buffer 41 61> (Buffer)

// compatibility mode handles only raw format both for String and Buffer
const options = {codec: msgpack.createCodec({useraw: true})};
msgpack.encode("Aa", options); // => <Buffer a2 41 61> (raw format)
msgpack.encode(Buffer.from([0x41, 0x61]), options); // => <Buffer a2 41 61> (raw format)

msgpack.decode(Buffer.from([0xa2, 0x41, 0x61]), options); // => <Buffer 41 61> (Buffer)
msgpack.decode(Buffer.from([0xa2, 0x41, 0x61]), options).toString(); // => 'Aa' (String)
```

### Repository

- [https://github.com/kawanet/msgpack-lite](https://github.com/kawanet/msgpack-lite)

### See Also

- [http://msgpack.org/](http://msgpack.org/)

### License

The MIT License (MIT)

Copyright (c) 2015-2026 Yusuke Kawasaki

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

---
_Source: https://npm.io/package/msgpack-lite · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
