{"id":25593,"library":"flatc-wasm","title":"flatc-wasm","description":"FlatBuffers compiler (flatc) compiled to WebAssembly, enabling schema management, JSON/binary conversion, and code generation in Node.js (18+) and modern browsers without native dependencies. Current stable version 26.1.32, released February 2025. Key differentiators: self-contained with inlined WASM binaries, zero native deps, supports 13 code generation languages (C++, TypeScript, Go, Rust, Python, Java, etc.), per-field AES-256-CTR encryption, streaming APIs for large data, JSON Schema import/export, and embedded language runtimes. Licensed under Apache 2.0. Maintained by DigitalArsenal on GitHub.","status":"active","version":"26.1.32","language":"javascript","source_language":"en","source_url":"https://github.com/DigitalArsenal/flatbuffers","tags":["javascript","flatbuffers","flatc","wasm","webassembly","serialization","schema","compiler","codegen","typescript"],"install":[{"cmd":"npm install flatc-wasm","lang":"bash","label":"npm"},{"cmd":"yarn add flatc-wasm","lang":"bash","label":"yarn"},{"cmd":"pnpm add flatc-wasm","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Optional dependency for HD key derivation used in encryption features","package":"hd-wallet-wasm","optional":true}],"imports":[{"note":"ES module import; also works with dynamic import()","wrong":"const FlatcRunner = require('flatc-wasm').FlatcRunner","symbol":"FlatcRunner","correct":"import { FlatcRunner } from 'flatc-wasm'"},{"note":"Low-level module factory for manual WASM initialization","wrong":"const createFlatcModule = require('flatc-wasm')","symbol":"createFlatcModule","correct":"import { createFlatcModule } from 'flatc-wasm'"},{"note":"Enum used for aligned binary format; type import also available: import type { AlignedType }","wrong":"import { AlignedTypes } from 'flatc-wasm'","symbol":"AlignedType","correct":"import { AlignedType } from 'flatc-wasm'"}],"quickstart":{"code":"import { FlatcRunner } from 'flatc-wasm';\nimport { readFileSync, writeFileSync } from 'fs';\n\nasync function main() {\n  // Create runner instance\n  const runner = await FlatcRunner.create();\n  \n  // Load a FlatBuffers schema\n  const schema = readFileSync('monster.fbs', 'utf8');\n  await runner.addSchema('monster', schema);\n  \n  // Convert JSON to FlatBuffer binary\n  const jsonData = JSON.stringify({ name: 'Orc', hp: 300 });\n  const binary = await runner.jsonToBinary('monster', jsonData, 'MyGame.Sample.Monster');\n  writeFileSync('monster.bin', Buffer.from(binary));\n  \n  // Convert binary back to JSON (auto-detects schema)\n  const binaryBuf = readFileSync('monster.bin');\n  const jsonOut = await runner.binaryToJson('monster', new Uint8Array(binaryBuf));\n  console.log(jsonOut);\n  \n  // Generate TypeScript code\n  const tsCode = await runner.generateCode('monster', 'typescript');\n  writeFileSync('monster_generated.ts', tsCode);\n}\n\nmain().catch(console.error);","lang":"typescript","description":"Demonstrates loading a FlatBuffers schema, converting between JSON and binary, and generating TypeScript code using FlatcRunner."},"warnings":[{"fix":"Use await FlatcRunner.create() instead of new FlatcRunner().","message":"WASM module initialization changed from synchronous to async in v26.0.0. FlatcRunner.create() must be awaited.","severity":"breaking","affected_versions":">=26.0.0"},{"fix":"Migrate to FlatcRunner API for easier usage and better error handling.","message":"The low-level API 'createFlatcModule' is deprecated in favor of 'FlatcRunner' since v25.12.0.","severity":"deprecated","affected_versions":">=25.12.0"},{"fix":"const buffer = Buffer.from(await runner.jsonToBinary(...));","message":"Binary output from jsonToBinary is an ArrayBuffer, not a Buffer. Use Buffer.from() before writing to file.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Upgrade Node.js to 18.0.0 or later.","message":"Node.js minimum version raised to 18.0.0 in v25.0.0. Older versions are not supported.","severity":"breaking","affected_versions":">=25.0.0"},{"fix":"Ensure schema name matches the file stem, e.g., 'monster' for 'monster.fbs'.","message":"Schema names are case-sensitive and must match the schema file name (without .fbs). Adding a schema with the wrong name will fail silently.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Ensure the WASM binary is served with the correct MIME type 'application/wasm' and loaded via fetch or import. In webpack, add a rule to load .wasm files as WebAssemblyModule.","cause":"The WASM binary is corrupted or not fully loaded due to network issues in browser or incorrect bundler configuration.","error":"Error: WebAssembly.instantiate(): expected magic word 00 61 73 6d"},{"fix":"Use const runner = await FlatcRunner.create();","cause":"FlatcRunner.create() not awaited; runner object is undefined.","error":"TypeError: Cannot read properties of undefined (reading 'addSchema')"},{"fix":"Call await runner.addSchema('monster', schemaContent) first.","cause":"Schema was not added before calling conversion or code generation functions.","error":"Error: Schema 'monster' not found"},{"fix":"Verify that the binary was generated by flatc or by jsonToBinary with the correct schema and root type.","cause":"ArrayBuffer passed to binaryToJson is too small or the binary format is malformed.","error":"RangeError: offset is out of bounds"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}