A robust, type-safe, and high-performance NBT (Named Binary Tag) library for JavaScript and TypeScript. Designed to be easy to use while providing full control over the NBT format. Supports both CommonJS and ES Modules.
- 🚀 High Performance: Optimized for speed and low memory usage.
- 📦 Dual Build: Works seamlessly with CommonJS (
require) and ES Modules (import). - 🔒 Type-Safe: Written in TypeScript with complete type definitions.
- 🗜️ Compression Support: Built-in support for Gzip compression/decompression.
- 🌐 Universal: Works in Node.js and modern environments supporting Buffers.
npm install js.nbtconst { parse, writeSync } = require("js.nbt");
const fs = require("fs");
// Reading
const buffer = fs.readFileSync("level.dat");
const nbt = parse(buffer);
// Writing
const newBuffer = writeSync(nbt);You can read NBT data from a file or buffer. The library automatically detects if the data is Gzipped.
import { parse, parseSync } from "js.nbt";
import * as fs from "fs";
// Async
async function readNbt() {
const buffer = await fs.promises.readFile("level.dat");
const nbt = await parse(buffer);
console.log(nbt);
}
// Sync
const buffer = fs.readFileSync("level.dat");
const nbt = parseSync(buffer);
console.log(nbt);You can create NBT data structures and serialize them back to a buffer.
import { write, writeSync, ParsedNBT } from "js.nbt";
import * as fs from "fs";
const data: ParsedNBT = {
name: "MyLevel",
value: {
LevelName: "New World",
SpawnX: 100,
SpawnY: 64,
SpawnZ: 100,
Player: {
Health: 20.0,
Inventory: [],
},
},
};
// Async (Compressed)
async function saveNbt() {
const buffer = await write(data, true); // true for Gzip compression
await fs.promises.writeFile("level.dat", buffer);
}
// Sync (Uncompressed)
const buffer = writeSync(data, false);
fs.writeFileSync("level_uncompressed.dat", buffer);The library maps NBT tags to JavaScript types as follows:
| NBT Tag | JavaScript Type |
|---|---|
| Byte | number |
| Short | number |
| Int | number |
| Long | bigint |
| Float | number |
| Double | number |
| ByteArray | Buffer |
| String | string |
| List | Array |
| Compound | Object |
| IntArray | Int32Array |
| LongArray | BigInt64Array |
Asynchronously parses an NBT buffer. Automatically handles Gzip decompression.
Synchronously parses an NBT buffer. Automatically handles Gzip decompression.
Asynchronously serializes NBT data. Set compress to true to Gzip the output.
Synchronously serializes NBT data. Set compress to true to Gzip the output.
MIT