{"record":{"id":"871bad35dd778c3e","repo":"evanw/esbuild","slug":"failed-to-download-json-stringify-wasm","errorCode":null,"errorMessage":"Failed to download ${JSON.stringify(wasm)}","messagePattern":"Failed to download (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/shared/worker.ts","lineNumber":90,"sourceCode":"    instance => {\n      postMessage(null)\n      go.run(instance)\n    },\n    error => {\n      postMessage(error)\n    },\n  )\n\n  return go\n}\n\nasync function tryToInstantiateModule(wasm: WebAssembly.Module | string, go: Go): Promise<WebAssembly.Instance> {\n  if (wasm instanceof WebAssembly.Module) {\n    return WebAssembly.instantiate(wasm, go.importObject)\n  }\n\n  const res = await fetch(wasm)\n  if (!res.ok) throw new Error(`Failed to download ${JSON.stringify(wasm)}`)\n\n  // Attempt to use the superior \"instantiateStreaming\" API first\n  if ('instantiateStreaming' in WebAssembly && /^application\\/wasm($|;)/i.test(res.headers.get('Content-Type') || '')) {\n    const result = await WebAssembly.instantiateStreaming(res, go.importObject)\n    return result.instance\n  }\n\n  // Otherwise, fall back to the inferior \"instantiate\" API\n  const bytes = await res.arrayBuffer()\n  const result = await WebAssembly.instantiate(bytes, go.importObject)\n  return result.instance\n}\n","sourceCodeStart":72,"sourceCodeEnd":103,"githubUrl":"https://github.com/evanw/esbuild/blob/f6058f8364fe7ab91ca57a83e02577ed74c9cae4/lib/shared/worker.ts#L72-L103","documentation":"In the WASM worker (lib/shared/worker.ts:84), `tryToInstantiateModule` fetches the esbuild WebAssembly binary by URL via `fetch(wasm)`. If the response is not ok (`res.ok` is false — any non-2xx status), it throws this error including the JSON-stringified `wasm` value (the URL or module descriptor). This is a download/load failure for the WASM service binary.","triggerScenarios":"Loading esbuild-wasm in a browser/worker where the WASM file URL is wrong, the server returns 404/403/500, the URL points to an unreachable host, or a CSP/network policy blocks the fetch.","commonSituations":"Misconfigured `wasmURL`/`wasmModule` option; serving the `.wasm` file from a CDN that rate-limits or is unreachable; bundler rewrites that break the asset URL; offline or air-gapped browser; CORS restrictions on the hosting origin.","solutions":["Verify the `wasmURL` resolves to a 200 response for the WASM file and that the host permits CORS.","Bundle the WASM file locally and pass `wasmURL` pointing to a same-origin asset path.","Pre-fetch the module yourself and pass it via `wasmModule` to avoid esbuild's fetch entirely.","Check network/CSP policies (connect-src) allow the WASM origin."],"exampleFix":"// before\nawait esbuild.initialize({ wasmURL: 'https://broken.cdn/esbuild.wasm' });\n// after — host locally / preloaded\nawait esbuild.initialize({ wasmURL: '/assets/esbuild.wasm' });\n// or\nconst mod = await WebAssembly.compile(await (await fetch('/assets/esbuild.wasm')).arrayBuffer());\nawait esbuild.initialize({ wasmModule: mod });","handlingStrategy":"retry","validationCode":"async function loadWasmModule(url: string): Promise<WebAssembly.Module> {\n  let res = await fetch(url);\n  if (!res.ok) throw new Error(`Failed to download ${JSON.stringify(url)} (status ${res.status})`);\n  return WebAssembly.compile(await res.arrayBuffer());\n}\nconst wasmModule = await loadWasmModule('/assets/esbuild.wasm');\nawait esbuild.initialize({ wasmModule });","typeGuard":null,"tryCatchPattern":"try {\n  await esbuild.initialize({ wasmURL });\n} catch (e) {\n  if (/Failed to download/.test(String(e.message))) { /* retry from a mirror, verify CORS, or preload wasmModule */ }\n  else throw e;\n}","preventionTips":["Host the WASM file same-origin and pass wasmURL.","Preload the module and pass wasmModule to bypass esbuild's fetch.","Ensure CSP connect-src allows the WASM URL."],"tags":["wasm","network","browser","download","fetch"],"backgroundTag":null,"analyzedSha":"f6058f8364fe7ab91ca57a83e02577ed74c9cae4","analyzedAt":"2026-08-09T18:37:22.223Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}