{"id":26635,"library":"watr","title":"watr","description":"watr is a lightweight, zero-dependency WAT (WebAssembly Text format) compiler written in JavaScript. Current stable version is 4.5.0, with frequent releases adding support for upcoming WebAssembly proposals. It compiles WAT to binary (wasm), parses into AST, pretty-prints, minifies, optimizes, and polyfills newer features to MVP. Key differentiators: small bundle size (~84KB minified, ~24KB gzipped), high throughput (~4460 op/s vs 700-2200 for alternatives), spec-complete with GC, exception handling, and stack switching, built-in CLI, and a unique tagged template literal for instant wasm exports with JS interop.","status":"active","version":"4.5.0","language":"javascript","source_language":"en","source_url":"https://github.com/dy/watr","tags":["javascript","wat","wasm","webassembly","compiler","wat2wasm","wabt","wast","parser","typescript"],"install":[{"cmd":"npm install watr","lang":"bash","label":"npm"},{"cmd":"yarn add watr","lang":"bash","label":"yarn"},{"cmd":"pnpm add watr","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM-only; watr default export is a tagged template function that compiles and instantiates WAT to WasmModule exports.","wrong":"const watr = require('watr')","symbol":"watr (default)","correct":"import watr from 'watr'"},{"note":"Named export; available since v3. Use compile(source, options) to get Uint8Array.","wrong":"import { compile } from 'watr/compile'","symbol":"compile","correct":"import { compile } from 'watr'"},{"note":"Named export; parse('(i32.const 42)') returns an AST array. Not the default export.","wrong":"import parse from 'watr'","symbol":"parse","correct":"import { parse } from 'watr'"},{"note":"Named export; print(ast, options) pretty-prints or minifies WAT. printTree is not an exported name.","wrong":"import { printTree } from 'watr'","symbol":"print","correct":"import { print } from 'watr'"},{"note":"TypeScript ships types; use type import for compile-time only.","wrong":"import { WatNode } from 'watr'","symbol":"Type definitions","correct":"import type { WatNode } from 'watr'"}],"quickstart":{"code":"import watr, { compile, parse, print } from 'watr'\n\n// 1. Compile WAT to binary\nconst binary = compile('(func (export \"f\") (result f64) (f64.const 1))', {\n  polyfill: false,\n  optimize: true\n})\nconst module = new WebAssembly.Module(binary)\nconst { f } = new WebAssembly.Instance(module).exports\nconsole.log(f()) // 1\n\n// 2. Parse WAT to AST tree\nconst tree = parse('(i32.const 42)')\nconsole.log(tree) // ['i32.const', 42]\n\n// 3. Print (pretty-print) WAT\nconst prettified = print('(module(func(result i32)i32.const 42))')\nconsole.log(prettified) // (module\\n  (func (result i32)\\n    i32.const 42\\n  )\\n)\n\n// 4. Use the tagged template for instant wasm\nconst { add } = watr`(func (export \"add\") (param i32 i32) (result i32)\n  (i32.add (local.get 0) (local.get 1))\n)`\nconsole.log(add(2, 3)) // 5\n\n// 5. Auto-import JS functions\nconst { test } = watr`(func (export \"test\") (call ${console.log} (i32.const 42)))`\ntest() // logs 42","lang":"typescript","description":"Compiles WAT to binary, parses to AST, pretty-prints, and uses tagged template for instant wasm with JS interop."},"warnings":[{"fix":"Update to use compile(source, options) which returns Uint8Array synchronously. If you used callbacks, switch to promise or synchronous usage.","message":"v4.0.0 removed several deprecated mnemonics and changed the compile API signature (removed old callback style).","severity":"breaking","affected_versions":">=4.0.0 <4.0.0"},{"fix":"Use interpolations for imports or constants only; do not embed dynamic code generation via string concatenation inside template literal — use compile() with string building instead.","message":"The tagged template literal watr`...` does NOT accept arbitrary JavaScript expressions inside WAT; only interpolations of JS functions or primitive values (BigInt, typed arrays, etc.) are supported.","severity":"gotcha","affected_versions":">=4.0.0"},{"fix":"Use polyfill: false/true for now; check changelog for future changes.","message":"The 'polyfill' option in compile() may be deprecated or renamed in future versions.","severity":"deprecated","affected_versions":">=4.0.0"},{"fix":"Use dynamic import: const watr = await import('watr'); or switch to ESM.","message":"ESM-only: CommonJS require() will fail with Module not found or default export missing.","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"Validate WAT against the WebAssembly spec; use the official test suite or watr's parse to check.","message":"v3.0.0 introduced full spec support, breaking many WAT snippets that relied on earlier non-standard sugar or missing features.","severity":"breaking","affected_versions":">=3.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Use `import watr from 'watr'` (ESM) or `const watr = await import('watr')` in Node with ESM enabled. If using TypeScript, ensure module resolution is 'node' or 'node16' and 'esModuleInterop' is true.","cause":"Using CommonJS require instead of ESM import.","error":"Cannot find module 'watr' or its corresponding type declarations"},{"fix":"Use `parse(yourWat)` to get detailed error with line/column. Check the WAT against the WebAssembly spec or use watr's print with formatting to debug.","cause":"Invalid WAT syntax; e.g., missing closing parenthesis or wrong instruction name.","error":"SyntaxError: Unexpected token ("},{"fix":"Use watr`...` (backticks) not watr('...'). For direct compilation, use the named `compile` import.","cause":"Trying to call the default export as a function instead of using it as a tagged template literal.","error":"TypeError: watr is not a function"},{"fix":"Upgrade to watr >=3.0.0 (currently 4.5.0) which fully supports reference types and all proposals.","cause":"Older version of watr (<3.0.0) does not support reference types.","error":"Error: Unknown type 'funcref'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}