mozilla/pdf.js · error · JpxError

OpenJPEG failed to initialize

Error message

OpenJPEG failed to initialize

What it means

Thrown by JpxImage.decode() when _getModule(OpenJPEG) returns a falsy module. _getModule lazily loads and instantiates the OpenJPEG WASM; a null/undefined result means the WASM module never initialized. This is an environment/setup failure, not an image-data problem.

Source

Thrown at src/core/jpx.js:48

  _noWasmFilename = "openjpeg_nowasm_fallback.js";

  static get instance() {
    return shadow(this, "instance", new JpxImage(/* trackInstance = */ true));
  }

  async decode(
    bytes,
    {
      numComponents = 4,
      isIndexedColormap = false,
      smaskInData = false,
      reducePower = 0,
    } = {}
  ) {
    const module = await this._getModule(OpenJPEG);

    if (!module) {
      throw new JpxError("OpenJPEG failed to initialize");
    }
    let ptr;

    try {
      const size = bytes.length;
      ptr = module._malloc(size);
      module.writeArrayToMemory(bytes, ptr);
      const ret = module._jp2_decode(
        ptr,
        size,
        numComponents > 0 ? numComponents : 0,
        !!isIndexedColormap,
        !!smaskInData,
        reducePower
      );
      if (ret) {
        const { errorMessages } = module;
        if (errorMessages) {

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Verify openjpeg.wasm (and the asm.js fallback) exist in the deployed assets directory next to pdf.worker.js.
  2. Add 'wasm-unsafe-eval' (or 'script-src' allowing the worker) to your Content-Security-Policy header.
  3. Confirm the browser supports WebAssembly (typeof WebAssembly !== 'undefined').
  4. Check network tab for 404s on the .wasm URL; fix the base path / CDN configuration.
Defensive patterns

Strategy: validation

Validate before calling

async function ensureWasm() {
  if (typeof WebAssembly === 'undefined') throw new Error('WebAssembly not available');
  // confirm the wasm asset is reachable
  const res = await fetch(wasmUrl, { method: 'HEAD' });
  if (!res.ok) throw new Error('openjpeg.wasm not reachable: ' + res.status);
}

Type guard

function wasmSupported() {
  return typeof WebAssembly === 'object' && typeof WebAssembly.instantiate === 'function';
}

Try / catch

try { imageData = await JpxImage.instance.decode(bytes); }
catch (e) { if (e.name === 'JpxError' && /initialize/.test(e.message)) { /* env/setup issue */ } else throw e; }

Prevention

When it happens

Trigger: Awaiting this._getModule(OpenJPEG) yields a falsy value. Happens when openjpeg.wasm cannot be fetched (404/network), WebAssembly isn't supported/enabled, a CSP blocks 'wasm-unsafe-eval' or worker src, or the WASM file is missing from the deployment.

Common situations: Self-hosted PDF.js where the build/ assets (openjpeg.wasm / openjpeg_nowasm_fallback.js) weren't copied alongside the JS bundle. A strict Content-Security-Policy without wasm-src. Older browsers or non-WASM Node environments. Misconfigured workerBasePath/baseUrl.

Related errors


AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13). Data as JSON: /api/errors/d9d313cd0ddf2c49. Report an issue: GitHub.