airbnb/lottie-web · error · Error

Cannot access DOM from worker thread

Error message

Cannot access DOM from worker thread

What it means

The worker override of the animation manager's searchAnimations() always throws. searchAnimations normally walks document for elements with data-anim-type attributes and auto-registers them, which is impossible in a Web Worker where document does not exist. The function is intentionally stubbed as an always-throw to fail fast on DOM-dependent API use.

Source

Thrown at player/js/animation/AnimationManagerWorkerOverride.js:154

    }
  }

  function togglePause(animation) {
    var i;
    for (i = 0; i < len; i += 1) {
      registeredAnimations[i].animation.togglePause(animation);
    }
  }

  function destroy(animation) {
    var i;
    for (i = (len - 1); i >= 0; i -= 1) {
      registeredAnimations[i].animation.destroy(animation);
    }
  }

  function searchAnimations() {
    throw new Error('Cannot access DOM from worker thread');
  }

  function resize() {
    var i;
    for (i = 0; i < len; i += 1) {
      registeredAnimations[i].animation.resize();
    }
  }

  function activate() {
    if (!_isFrozen && playingAnimationsNum) {
      if (_stopped) {
        requestAnimationFrame(first);
        _stopped = false;
      }
    }
  }

View on GitHub (pinned to bede03d25d)

Solutions

  1. Do not call lottie.searchAnimations() in a worker; use lottie.loadAnimation({ renderer: 'canvas', animationData }) instead.
  2. Branch on environment: only call searchAnimations when typeof document !== 'undefined'.
  3. Keep DOM-based auto-discovery on the main thread and post animationData to the worker for rendering.

Example fix

// before (runs in worker build)
lottie.searchAnimations();
// after
if (typeof document !== 'undefined') {
  lottie.searchAnimations();
} else {
  lottie.loadAnimation({ renderer: 'canvas', animationData });
}
Defensive patterns

Strategy: validation

Validate before calling

// searchAnimations is a DOM API; never call it in a worker.
function autoDiscover() {
  if (typeof document === 'undefined') {
    throw new Error('searchAnimations requires the DOM; use loadAnimation in a worker.');
  }
  return lottie.searchAnimations();
}

Type guard

function canSearchDom() {
  return typeof document !== 'undefined' && typeof document.getElementsByTagName === 'function';
}

Prevention

When it happens

Trigger: Calling lottie.searchAnimations() (directly, or via the main module's auto-search/standalone path) while running inside the worker build. Also reachable through any shared code that calls the manager's searchAnimations.

Common situations: Including the worker build in code that also calls searchAnimations for auto-discovery; running a generic lottie integration module in both main thread and worker without branching; standalone build flow that triggers auto-search.

Related errors


AI-assisted analysis of airbnb/lottie-web@bede03d25d (2026-08-13). Data as JSON: /api/errors/af1d55b120e8e660. Report an issue: GitHub.