{"id":"95835fce87442db6","repo":"vitejs/vite","slug":"failed-to-parse-wasm-file-wasmfilepath-e","errorCode":null,"errorMessage":"Failed to parse WASM file \"${wasmFilePath}\": ${(e as Error).message}","messagePattern":"Failed to parse WASM file \"(.+?)\": (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vite/src/node/plugins/wasm.ts","lineNumber":263,"sourceCode":"    const importMap = new Map<string, WasmName[]>()\n    for (const item of WebAssembly.Module.imports(wasmModule)) {\n      if (wasmReservedModules.has(item.module)) continue\n      let names = importMap.get(item.module)\n      if (!names) importMap.set(item.module, (names = []))\n      names.push({ name: item.name, isGlobal: item.kind === 'global' })\n    }\n    const imports = [...importMap].map(([from, names]) => ({ from, names }))\n\n    let hasGlobalExport = false\n    const exports = WebAssembly.Module.exports(wasmModule).map((item) => {\n      const isGlobal = item.kind === 'global'\n      if (isGlobal) hasGlobalExport = true\n      return { name: item.name, isGlobal }\n    })\n\n    return { imports, exports, hasGlobalExport }\n  } catch (e) {\n    throw new Error(\n      `Failed to parse WASM file \"${wasmFilePath}\": ${(e as Error).message}`,\n      { cause: e },\n    )\n  }\n}\n\n// Instantiates the wasm module and re-exports its exports verbatim. Globals stay\n// WebAssembly.Global objects so wasm-to-wasm global imports get the live cell.\nfunction generateInstanceGlue(\n  wasmInfo: WasmInfo,\n  names: { initWasm: string; wasmUrl: string },\n): string {\n  const importStatements: string[] = []\n  const importObject: SimpleObject = wasmInfo.imports.map(\n    ({ from, names: importNames }, i) => {\n      const value: SimpleObject = []\n      const globals = importNames.filter((n) => n.isGlobal)\n      const others = importNames.filter((n) => !n.isGlobal)","sourceCodeStart":245,"sourceCodeEnd":281,"githubUrl":"https://github.com/vitejs/vite/blob/89620f09afcfef6b35e7bb8660132ab5b4d0cd3b/packages/vite/src/node/plugins/wasm.ts#L245-L281","documentation":"parseWasm (packages/vite/src/node/plugins/wasm.ts:241) reads the .wasm file, calls WebAssembly.compile with Vite's wasm-compiled-options (js-string builtins), then enumerates Module.imports/exports to generate glue code. Any failure — corrupt bytes, unsupported proposal, or a compile error — is re-thrown with the original as `cause` and the file path in the message.","triggerScenarios":"Importing a .wasm that is truncated/corrupt; a wasm built with a proposal your host Node version does not support; a non-wasm file accidentally named .wasm; a wasm whose imports reference modules the engine rejects during Module.imports inspection.","commonSituations":"Bumping @vitejs/plugin-wasm or the toolchain that emits wasm (wasm-pack, emscripten, assemblyscript) to a version using newer proposals; checking out a repo with a stale or partially-downloaded .wasm artifact; case-sensitivity issues on Linux pointing at a non-existent file that fsp.readFile rejects.","solutions":["Re-build/re-download the .wasm so its bytes are complete and valid; verify with `node -e \"WebAssembly.compile(require('fs').readFileSync('x.wasm'))\"`.","Use a Node version that supports the wasm proposals the module was compiled with (check the release that introduced js-string builtins / the proposal in question).","Confirm the file is actually WebAssembly (`file x.wasm` / check magic bytes \\0asm) and that Vite resolves the path you expect.","If you do not need Vite's glue generation, import the wasm via ?init and instantiate it manually so parseWasm is bypassed."],"exampleFix":"// before: direct import triggers parseWasm and fails on an old Node\nimport wasm from './asset.wasm'\n\n// after: validate/upgrade the toolchain, then keep the import\n//   node --version  # upgrade to a release with the needed proposal\n//   wasm-pack build --target web\nimport wasm from './asset.wasm'","handlingStrategy":"validation","validationCode":"import { readFileSync } from 'node:fs'\nfunction wasmLooksValid(file: string): boolean {\n  const b = readFileSync(file)\n  return b.length >= 8 && b[0] === 0x00 && b[1] === 0x61 && b[2] === 0x73 && b[3] === 0x6d\n}\nif (!wasmLooksValid('asset.wasm')) throw new Error('not a valid wasm file')","typeGuard":"function isWasmMagic(bytes: Uint8Array): boolean {\n  return bytes[0] === 0x00 && bytes[1] === 0x61 && bytes[2] === 0x73 && bytes[3] === 0x6d\n}","tryCatchPattern":"try {\n  await import('./asset.wasm')\n} catch (e) {\n  if (/Failed to parse WASM file/.test((e as Error).message)) {\n    // rebuild wasm artifact / upgrade Node, then retry build\n  }\n  throw e\n}","preventionTips":["Validate wasm magic bytes before importing in CI.","Pin the toolchain (wasm-pack/ emscripten) and Node version that together produce supported proposals.","Treat .wasm as a build artifact: rebuild on toolchain upgrades rather than committing stale binaries."],"tags":["wasm","build","toolchain","node-version"],"analyzedSha":"89620f09afcfef6b35e7bb8660132ab5b4d0cd3b","analyzedAt":"2026-08-03T19:28:02.920Z","schemaVersion":2}