{"record":{"id":"88403b48ffda2f6b","repo":"golang/go","slug":"globalthis-textencoder-is-not-available-polyfill","errorCode":null,"errorMessage":"globalThis.TextEncoder is not available, polyfill required","messagePattern":"globalThis\\.TextEncoder is not available, polyfill required","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"lib/wasm/wasm_exec.js","lineNumber":93,"sourceCode":"\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 {\n\t\tconstructor() {\n\t\t\tthis.argv = [\"js\"];\n\t\t\tthis.env = {};\n\t\t\tthis.exit = (code) => {\n\t\t\t\tif (code !== 0) {\n\t\t\t\t\tconsole.warn(\"exit code:\", code);\n\t\t\t\t}\n\t\t\t};","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/lib/wasm/wasm_exec.js#L75-L111","documentation":"Thrown by wasm_exec.js when globalThis.TextEncoder is not present. TextEncoder is used to convert JS strings to UTF-8 bytes for passing into Go's wasm memory. It is a load-time gate, so the failure happens the moment wasm_exec.js is evaluated, before any Go function can be called.","triggerScenarios":"Running in a JS engine that does not ship the Encoding Living Standard API: very old Node (< 11), some embedded engines, or a VM context that did not inherit TextEncoder.","commonSituations":"Calling Go wasm from a legacy Node runtime; evaluating the support file inside a fresh vm context; targeting an IoT/embedded JS engine with no text-encoding module.","solutions":["Upgrade to Node.js >= 11 (TextEncoder is global by default).","Polyfill from util: globalThis.TextEncoder = require('node:util').TextEncoder; before requiring wasm_exec.js.","Install the text-encoding npm package and assign globalThis.TextEncoder = require('text-encoding').TextEncoder;.","For vm contexts, pass { TextEncoder } into the sandbox object passed to vm.createContext."],"exampleFix":"// before\nrequire('./wasm_exec.js'); // throws: globalThis.TextEncoder is not available\n\n// after\nconst { TextEncoder } = require('node:util');\nglobalThis.TextEncoder = TextEncoder;\nrequire('./wasm_exec.js');","handlingStrategy":"validation","validationCode":"if (typeof globalThis.TextEncoder === 'undefined') {\n  globalThis.TextEncoder = require('node:util').TextEncoder;\n}","typeGuard":"const hasTextEncoder = () => typeof globalThis.TextEncoder === 'function';","tryCatchPattern":null,"preventionTips":["Use Node >= 11 (TextEncoder is global).","Centralize all polyfills in one bootstrap file imported first.","Propagate TextEncoder into vm contexts via the sandbox object."],"tags":["wasm","go","textencoder","polyfill","runtime","environment"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}