{"record":{"id":"e9d43deb85b87ce6","repo":"golang/go","slug":"globalthis-crypto-is-not-available-polyfill-requi","errorCode":null,"errorMessage":"globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)","messagePattern":"globalThis\\.crypto is not available, polyfill required \\(crypto\\.getRandomValues only\\)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"lib/wasm/wasm_exec.js","lineNumber":85,"sourceCode":"\t\t\tgetgroups() { throw enosys(); },\n\t\t\tpid: -1,\n\t\t\tppid: -1,\n\t\t\tumask() { throw enosys(); },\n\t\t\tcwd() { throw enosys(); },\n\t\t\tchdir() { throw enosys(); },\n\t\t}\n\t}\n\n\tif (!globalThis.path) {\n\t\tglobalThis.path = {\n\t\t\tresolve(...pathSegments) {\n\t\t\t\treturn pathSegments.join(\"/\");\n\t\t\t}\n\t\t}\n\t}\n\n\tif (!globalThis.crypto) {\n\t\tthrow new Error(\"globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)\");\n\t}\n\n\tif (!globalThis.performance) {\n\t\tthrow new Error(\"globalThis.performance is not available, polyfill required (performance.now only)\");\n\t}\n\n\tif (!globalThis.TextEncoder) {\n\t\tthrow new Error(\"globalThis.TextEncoder is not available, polyfill required\");\n\t}\n\n\tif (!globalThis.TextDecoder) {\n\t\tthrow new Error(\"globalThis.TextDecoder is not available, polyfill required\");\n\t}\n\n\tconst encoder = new TextEncoder(\"utf-8\");\n\tconst decoder = new TextDecoder(\"utf-8\");\n\n\tglobalThis.Go = class {","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/lib/wasm/wasm_exec.js#L67-L103","documentation":"Thrown by the Go WebAssembly support file (wasm_exec.js) during startup initialization when globalThis.crypto is missing. The Go wasm runtime needs crypto.getRandomValues to seed its deterministic PRNG used for map iteration and goroutine scheduling. Without it, the runtime cannot guarantee correct operation. The error is a hard gate, not a warning.","triggerScenarios":"Loading wasm_exec.js in a JS environment that does not expose the Web Crypto API on globalThis (older Node.js < 15, non-browser runtimes like bare QuickJS, locked-down sandboxes, or VM contexts where globals are not propagated).","commonSituations":"Running Go-compiled .wasm in Node < 15 without the global crypto polyfill; evaluating wasm_exec.js inside a `vm.createContext` that did not inherit crypto; running in an embedded JS engine (Duktape, JerryScript) with no WebCrypto.","solutions":["Run on Node.js >= 19 where globalThis.crypto is exposed by default via the webcrypto module.","Before importing wasm_exec.js, set globalThis.crypto = require('node:crypto').webcrypto;","If only getRandomValues is needed, polyfill: globalThis.crypto = { getRandomValues: (arr) => { nodeCrypto.randomFillSync(arr); return arr; } }; using the node:crypto module.","When using a VM context, pass { crypto: require('node:crypto').webcrypto } into vm.createContext so the global propagates."],"exampleFix":"// before\nrequire('./wasm_exec.js'); // throws: globalThis.crypto is not available\n\n// after\nconst { webcrypto } = require('node:crypto');\nglobalThis.crypto = webcrypto;\nrequire('./wasm_exec.js');","handlingStrategy":"validation","validationCode":"if (typeof globalThis.crypto === 'undefined' || typeof globalThis.crypto.getRandomValues !== 'function') {\n  const { webcrypto } = require('node:crypto');\n  globalThis.crypto = webcrypto;\n}","typeGuard":"const hasCrypto = () => typeof globalThis.crypto === 'object' && globalThis.crypto !== null && typeof globalThis.crypto.getRandomValues === 'function';","tryCatchPattern":null,"preventionTips":["Run Go-wasm host code on Node >= 19 where globalThis.crypto is provided by default.","Centralize polyfill checks in a single bootstrap module imported before wasm_exec.js.","When using vm.createContext, explicitly pass crypto into the sandbox so it propagates to globalThis."],"tags":["wasm","go","crypto","polyfill","runtime","environment"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}