mozilla/pdf.js · error · Error

field.numItems is read-only

Error message

field.numItems is read-only

What it means

Thrown by the setter of `Field.numItems` (src/scripting_api/field.js:176). The item count is derived from the underlying `_items` array and cannot be set directly; to change it you must use `setItems`, `insertItemAt`, `deleteItemAt`, or `clearItems`.

Source

Thrown at src/scripting_api/field.js:176

    return this._charLimit;
  }

  set charLimit(limit) {
    if (typeof limit !== "number") {
      throw new Error("Invalid argument value");
    }
    this._charLimit = Math.max(0, Math.floor(limit));
  }

  get numItems() {
    if (!this._isChoice) {
      throw new Error("Not a choice widget");
    }
    return this._items.length;
  }

  set numItems(_) {
    throw new Error("field.numItems is read-only");
  }

  get strokeColor() {
    return this._strokeColor;
  }

  set strokeColor(color) {
    if (Color._isValidColor(color)) {
      this._strokeColor = color;
    }
  }

  get borderColor() {
    return this.strokeColor;
  }

  set borderColor(color) {
    this.strokeColor = color;

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Replace the assignment with a call to `f.setItems(newArray)` whose length gives the desired count.
  2. Remove the assignment entirely if it was unintended.
  3. Wrap in try/catch if the write comes from generic serialization code you cannot refactor.

Example fix

// before
f.numItems = newValues.length;
// after
f.setItems(newValues); // numItems follows from the array length
Defensive patterns

Strategy: try-catch

Validate before calling

const READ_ONLY_FIELD_PROPS = new Set(['page','numItems']);
function isWritableFieldProp(key) {
  return !READ_ONLY_FIELD_PROPS.has(key);
}

Try / catch

try {
  f.numItems = n;
} catch (e) {
  // numItems is read-only; rebuild via setItems instead
  f.setItems(new Array(n).fill(''));
}

Prevention

When it happens

Trigger: Executing `f.numItems = 5;` to try to resize a list. Most often a copy/paste mistake or generic 'reset all properties' logic that round-trips the current value back through the setter.

Common situations: Form-state cloning code; scripts written for an older reader that tolerated the assignment; mis-typed `f.numItems == 5` (== vs =) silently assigning.

Related errors


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