dianping/cat · error · Error

Invalid dropzone element.

Error message

Invalid dropzone element.

What it means

The Dropzone constructor accepts a selector string or DOM element. It resolves the selector with document.querySelector, then requires the result to be a real DOM node (nodeType present). If the selector matched nothing (null) or an invalid element was passed, it throws 'Invalid dropzone element.' before doing anything else.

Source

Thrown at cat-home/src/main/webapp/assets/js/uncompressed/dropzone.js:579

          target[key] = val;
        }
      }
      return target;
    };

    function Dropzone(element, options) {
      var elementOptions, fallback, _ref;
      this.element = element;
      this.version = Dropzone.version;
      this.defaultOptions.previewTemplate = this.defaultOptions.previewTemplate.replace(/\n*/g, "");
      this.clickableElements = [];
      this.listeners = [];
      this.files = [];
      if (typeof this.element === "string") {
        this.element = document.querySelector(this.element);
      }
      if (!(this.element && (this.element.nodeType != null))) {
        throw new Error("Invalid dropzone element.");
      }
      if (this.element.dropzone) {
        throw new Error("Dropzone already attached.");
      }
      Dropzone.instances.push(this);
      this.element.dropzone = this;
      elementOptions = (_ref = Dropzone.optionsForElement(this.element)) != null ? _ref : {};
      this.options = extend({}, this.defaultOptions, elementOptions, options != null ? options : {});
      if (this.options.forceFallback || !Dropzone.isBrowserSupported()) {
        return this.options.fallback.call(this);
      }
      if (this.options.url == null) {
        this.options.url = this.element.getAttribute("action");
      }
      if (!this.options.url) {
        throw new Error("No URL provided.");
      }
      if (this.options.acceptedFiles && this.options.acceptedMimeTypes) {

View on GitHub (pinned to e815e74d4c)

Solutions

  1. Verify the element exists before constructing: if (!document.querySelector(sel)) return;
  2. Initialize after the element is in the DOM — move the script below the markup or hook the code that renders the form.
  3. Check the selector for typos and that autoDiscover's .dropzone class is actually on the intended element.

Example fix

// before
new Dropzone('#upload-form'); // element not yet in DOM

// after
$(function () {
  var el = document.querySelector('#upload-form');
  if (el && !el.dropzone) {
    new Dropzone(el, { url: '/upload' });
  }
});
Defensive patterns

Strategy: type-guard

Validate before calling

function resolveDropzoneTarget(sel) {
  var el = typeof sel === 'string' ? document.querySelector(sel) : sel;
  return (el && el.nodeType === 1) ? el : null;
}
var target = resolveDropzoneTarget('#upload');
if (target) new Dropzone(target, { url: '/upload' });

Type guard

function isAttachedElement(el) {
  return !!el && typeof el === 'object' && el.nodeType === 1 && el.ownerDocument;
}

Try / catch

try { dz = new Dropzone(sel, opts); } catch (e) {
  if (/Invalid dropzone element/.test(e.message)) {
    console.warn('Upload form not rendered yet; deferring init');
    deferredInit.push({ sel: sel, opts: opts });
  } else throw e;
}

Prevention

When it happens

Trigger: new Dropzone('#myUpload') or Dropzone.create('#myUpload') where #myUpload does not exist in the DOM at call time; initializing inside $(document).ready but the element is created later by AJAX; typo'd selector; passing undefined/null because a variable was not set.

Common situations: Initializing dropzone in a script that runs before the form is rendered (script at top of page, or content injected dynamically); SPA-ish pages where the upload form appears after the picker script ran; copy-paste of example selectors without adapting them.

Related errors


AI-assisted analysis of dianping/cat@e815e74d4c (2026-08-14). Data as JSON: /api/errors/095641608b7a8f48. Report an issue: GitHub.