{"id":24999,"library":"binaryen","title":"Binaryen.js","description":"Binaryen.js provides prebuilt browser and Node.js binaries of Binaryen, a compiler infrastructure and toolchain library for WebAssembly. Version 129.0.0 is the latest stable release; the project follows Binaryen releases closely. It optimizes Wasm modules, performs transformations (e.g., inlining, dead code elimination), and validates/parses Wasm. Key differentiators: it works entirely in JavaScript with no native dependencies, making it suitable for web build pipelines, and it supports both ESM and CommonJS. Compared to wasm-opt CLI, it enables programmatic Wasm optimization in Node.js or browser bundlers.","status":"active","version":"129.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/AssemblyScript/binaryen.js","tags":["javascript","webassembly","wasm","typescript"],"install":[{"cmd":"npm install binaryen","lang":"bash","label":"npm"},{"cmd":"yarn add binaryen","lang":"bash","label":"yarn"},{"cmd":"pnpm add binaryen","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Default export in ESM is a namespace object; call Binaryen.ready() before use (Async).","symbol":"Binaryen","correct":"import Binaryen from 'binaryen'"},{"note":"Module is a class on the default export; not a named export.","symbol":"Module","correct":"import Binaryen from 'binaryen'; const mod = new Binaryen.Module()"},{"note":"readBinary is attached to the default export, not imported standalone.","symbol":"readBinary (via Binaryen)","correct":"import Binaryen from 'binaryen'; const wasmBuffer = Binaryen.readBinary(new Uint8Array([...]))"}],"quickstart":{"code":"import Binaryen from 'binaryen';\n\nasync function main() {\n  await Binaryen.ready(); // ensure runtime is initialized\n  const module = new Binaryen.Module();\n  const funcType = module.addFunctionType('i32', Binaryen.i32, []);\n  const func = module.addFunction('add', funcType, [], [\n    module.local.get(0, Binaryen.i32),\n    module.local.get(1, Binaryen.i32),\n    module.i32.add()\n  ]);\n  module.addFunctionExport('add', 'add');\n  module.validate();\n  const binary = module.emitBinary();\n  console.log('Wasm binary size:', binary.length, 'bytes');\n}\nmain();","lang":"typescript","description":"Creates a simple Wasm module with an 'add' function, validates, and emits binary."},"warnings":[{"fix":"Always await Binaryen.ready() at top of your script.","message":"Binaryen.ready() must be called and awaited before any other API calls; failure leads to 'Binaryen is not initialized' errors.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use import syntax or upgrade to Node >=12.22 or use dynamic import().","message":"The Binaryen npm package is ESM-only starting from version 100.0.0; CommonJS require() will fail.","severity":"gotcha","affected_versions":">=100.0.0"},{"fix":"Consider offloading to a Web Worker or use Async variants if available (currently no async API, but Binaryen.ready is async).","message":"The API is synchronous but built on WebAssembly; thread-blocking operations may impact performance on the main thread.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Check the type definition file (.d.ts) for completeness; use 'as any' if needed.","message":"TypeScript types are provided but may lag behind the Native API; some methods may be missing or incorrectly typed.","severity":"gotcha","affected_versions":"<110.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Add 'await Binaryen.ready();' before any other Binaryen calls.","cause":"Forgetting to await Binaryen.ready() before using Binaryen.Module.","error":"TypeError: Binaryen.Module is not a constructor"},{"fix":"Ensure Binaryen.ready() is called and awaited at the start of your async function.","cause":"Binaryen.ready() not called or not awaited.","error":"Error: Binaryen is not initialized"},{"fix":"Switch to 'import Binaryen from 'binaryen'' and use an ESM-compatible setup.","cause":"Using CommonJS require() on an ESM-only package (version >=100).","error":"require() of ES Module ... not supported"},{"fix":"Ensure all function types, functions, and imports are added before exports.","cause":"Incorrect order of module construction (e.g., adding function export before function).","error":"Module \"...\" has been added to an invalid position"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}