01Install#
Any ES-module-capable JavaScript runtime. The WebAssembly module initializes automatically on the first call and is reused afterwards.
npm install @packvium/browser02Call pack() and read the result#
pack() takes a plain object and resolves to one — async only because the WebAssembly module has to finish loading first, not because packing itself is slow.
import { pack } from '@packvium/browser';
const result = await pack({
items: [{
id: 'book', quantity: 4,
dimensions: { length: '210', width: '140', height: '30' },
}],
containers: [{
id: 'carton',
inner_dimensions: { length: '400', width: '300', height: '250' },
}],
});
console.log(result.status); // "feasible"03Handle items the engine could not place#
Every entry in result.unpacked_items carries a stable reason string and the id of the item it refers to — the same shape @packvium/engine returns in Node, because both speak the one request/result contract.
for (const unpacked of result.unpacked_items) {
console.log(unpacked.item_id, unpacked.reason);
}04What is already checked before you see it#
Every placement pack() returns has been re-checked by validation logic independent of the search that found it. The fixture pages on this site show that validator's verdict next to the engine's for the same request, so a wrong placement is not something you have to take on trust.
05Check a result yourself#
From 1.7.0 the reference validator ships in the package. validate(request, result) resolves to every issue it finds, each with a code and a detail, and an empty array means the result is valid. It adds about 148 KB gzipped to the WebAssembly module, and from 1.7.0 the package needs Node 18 or later when it runs outside a browser.
import { pack, validate } from '@packvium/browser';
const result = await pack(request);
const issues = await validate(request, result);
for (const issue of issues) console.log(issue.code, issue.detail);06Write a result as JSON#
From 1.9.0 a weight is exact up to 2^63 - 1 ticks. A weight's ticks, and a shipping_cost score position that sums weights, stay a Number up to 2^53 - 1 ticks (about 1,126 tonnes in all) and become a BigInt past that, which JSON.stringify refuses. stringifyExact and parseExact carry such a result through JSON text exactly; for any smaller load they write what JSON.stringify writes.
import { pack, parseExact, stringifyExact } from '@packvium/browser';
const text = stringifyExact(await pack(request)); // JSON.stringify throws on a BigInt
const result = parseExact(text); // long integers come back exact07Next steps#
Read the request and result contract for every public field, run your own dimensions in the Playground — this site's own build of the same package — or open examples/shapes.mjs for items that are not their own bounding box.