golang/go · critical · Error
globalThis.crypto is not available, polyfill required (crypt
Error message
globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)
What it means
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.
Source
Thrown at lib/wasm/wasm_exec.js:85
getgroups() { throw enosys(); },
pid: -1,
ppid: -1,
umask() { throw enosys(); },
cwd() { throw enosys(); },
chdir() { throw enosys(); },
}
}
if (!globalThis.path) {
globalThis.path = {
resolve(...pathSegments) {
return pathSegments.join("/");
}
}
}
if (!globalThis.crypto) {
throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");
}
if (!globalThis.performance) {
throw new Error("globalThis.performance is not available, polyfill required (performance.now only)");
}
if (!globalThis.TextEncoder) {
throw new Error("globalThis.TextEncoder is not available, polyfill required");
}
if (!globalThis.TextDecoder) {
throw new Error("globalThis.TextDecoder is not available, polyfill required");
}
const encoder = new TextEncoder("utf-8");
const decoder = new TextDecoder("utf-8");
globalThis.Go = class {View on GitHub (pinned to b6b368adc5)
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.
Example fix
// before
require('./wasm_exec.js'); // throws: globalThis.crypto is not available
// after
const { webcrypto } = require('node:crypto');
globalThis.crypto = webcrypto;
require('./wasm_exec.js'); Defensive patterns
Strategy: validation
Validate before calling
if (typeof globalThis.crypto === 'undefined' || typeof globalThis.crypto.getRandomValues !== 'function') {
const { webcrypto } = require('node:crypto');
globalThis.crypto = webcrypto;
} Type guard
const hasCrypto = () => typeof globalThis.crypto === 'object' && globalThis.crypto !== null && typeof globalThis.crypto.getRandomValues === 'function';
Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- globalThis.performance is not available, polyfill required (
- globalThis.TextEncoder is not available, polyfill required
- globalThis.TextDecoder is not available, polyfill required
- Go.run: WebAssembly.Instance expected
- total length of command line and environment variables excee
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/e9d43deb85b87ce6.
Report an issue: GitHub.