golang/go · critical · Error

globalThis.TextEncoder is not available, polyfill required

Error message

globalThis.TextEncoder is not available, polyfill required

What it means

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.

Source

Thrown at lib/wasm/wasm_exec.js:93

	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 {
		constructor() {
			this.argv = ["js"];
			this.env = {};
			this.exit = (code) => {
				if (code !== 0) {
					console.warn("exit code:", code);
				}
			};

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Upgrade to Node.js >= 11 (TextEncoder is global by default).
  2. Polyfill from util: globalThis.TextEncoder = require('node:util').TextEncoder; before requiring wasm_exec.js.
  3. Install the text-encoding npm package and assign globalThis.TextEncoder = require('text-encoding').TextEncoder;.
  4. For vm contexts, pass { TextEncoder } into the sandbox object passed to vm.createContext.

Example fix

// before
require('./wasm_exec.js'); // throws: globalThis.TextEncoder is not available

// after
const { TextEncoder } = require('node:util');
globalThis.TextEncoder = TextEncoder;
require('./wasm_exec.js');
Defensive patterns

Strategy: validation

Validate before calling

if (typeof globalThis.TextEncoder === 'undefined') {
  globalThis.TextEncoder = require('node:util').TextEncoder;
}

Type guard

const hasTextEncoder = () => typeof globalThis.TextEncoder === 'function';

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/88403b48ffda2f6b. Report an issue: GitHub.