{"record":{"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/b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab/packages/vite/src/node/plugins/wasm.ts#L245-L281","documentation":"parseWasm reads the .wasm file and runs WebAssembly.compile plus Module.imports/exports to discover its import/export surface for glue generation. If compile or introspection throws (corrupt binary, truncated file, unsupported wasm feature), Vite rethrows with the wasm file path and the underlying error message as cause.","triggerScenarios":"Importing a .wasm file that is not a valid WebAssembly module: truncated download, wrong file type saved with a .wasm extension, a wasm using features the runtime rejects, or a zero-byte/corrupt file. parseWasm runs during the wasm plugin's transform of the importing module.","commonSituations":"Broken build artifact, git-lfs not pulling the binary, copy error leaving a partial file, or a wasm produced by a toolchain version emitting extensions (e.g. reference types) the host WebAssembly does not support.","solutions":["Inspect the underlying error (e.cause) for the exact WebAssembly error (e.g. 'magic number' mismatch means wrong file type).","Rebuild/re-download the wasm from source and confirm the byte size matches.","Ensure the runtime/Node version supports the wasm features the module uses.","Verify the file is genuinely wasm (xxd the first bytes for the magic 0x00 0x61 0x73 0x6d)."],"exampleFix":"// before — file is corrupt or wrong type saved as .wasm\nimport wasm from './pkg/my.wasm'\n// after — regenerate the wasm from source (e.g.)\n// wasm-pack build --target web\n// then confirm header bytes are 00 61 73 6d","handlingStrategy":"validation","validationCode":"import { readFileSync } from 'node:fs'\nfunction assertValidWasm(file) {\n  const buf = readFileSync(file)\n  const isWasm = buf.length >= 4 && buf[0]===0x00 && buf[1]===0x61 && buf[2]===0x73 && buf[3]===0x6d\n  if (!isWasm) throw new Error(`${file} is not a valid wasm module (bad magic)`)\n}","typeGuard":"function hasWasmMagic(buf: Uint8Array): boolean {\n  return buf.length >= 4 && buf[0]===0x00 && buf[1]===0x61 && buf[2]===0x73 && buf[3]===0x6d\n}","tryCatchPattern":"try {\n  return await parseWasm(file)\n} catch (e) {\n  logger.error(`WASM parse failed for ${file}: ${e.cause?.message ?? e.message}`)\n  throw e\n}","preventionTips":["Verify wasm artifacts produced by build tools each release.","Ensure git-lfs / copy steps preserve binary integrity.","Confirm the host WebAssembly supports the features your module uses."],"tags":["wasm","binary","build","webassembly"],"backgroundTag":null,"analyzedSha":"b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab","analyzedAt":"2026-08-11T11:49:19.515Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}