mozilla/pdf.js · error · Error
app.constants is read-only
Error message
app.constants is read-only
What it means
app.constants is a lazily-built, Object.freeze'd namespace (currently exposing align.left/center/right/top/bottom). Because it is a fixed enumeration surface in the Acrobat JS API, PDF.js freezes it and makes the setter throw. Any assignment to app.constants is rejected.
Source
Thrown at src/scripting_api/app.js:213
set calculate(calculate) {
this._document.obj.calculate = calculate;
}
get constants() {
return (this._constants ??= Object.freeze({
align: Object.freeze({
left: 0,
center: 1,
right: 2,
top: 3,
bottom: 4,
}),
}));
}
set constants(_) {
throw new Error("app.constants is read-only");
}
get focusRect() {
return this._focusRect;
}
set focusRect(val) {
/* TODO or not */
this._focusRect = val;
}
get formsVersion() {
return FORMS_VERSION;
}
set formsVersion(_) {
throw new Error("app.formsVersion is read-only");
}View on GitHub (pinned to 5903d58d58)
Solutions
- Do not assign to app.constants; treat it as a frozen enumeration.
- Define your own module-level constants object for any custom values.
- Read app.constants.align.<name> only for the supported keys (left, center, right, top, bottom).
Example fix
// before
app.constants = { align: { left: 0 } };
// after
const myAlign = { left: 0 }; // keep custom constants separate Defensive patterns
Strategy: validation
Validate before calling
// app.constants is frozen and read-only.
function safeConstant(app, group, key) {
const c = app.constants;
if (!c || !Object.isFrozen(c)) throw new Error('Unexpected constants shape');
return c?.[group]?.[key];
} Type guard
const isReadOnlyAppProp = (prop) => Object.getOwnPropertyDescriptor(App.prototype, prop)?.set?.name === ''; // (setter is an arrow-less throw stub; alternatively maintain an explicit list)
Try / catch
try { /* code that may assign app.constants */ }
catch (e) { if (!/constants is read-only/.test(e.message)) throw e; } Prevention
- Maintain a denylist of read-only app.* property names and statically check PDF JS against it.
- Use Object.freeze on your own constants rather than reusing app.constants.
- Read app.constants.align.* only for the documented keys.
When it happens
Trigger: PDF JavaScript or test code runs app.constants = {...} or attempts app.constants.align = .... Reassigning the property triggers the setter throw at src/scripting_api/app.js:213; mutating inner frozen objects throws a separate TypeError from Object.freeze.
Common situations: Scripts that try to extend the constants enumeration with custom alignment values; authors confusing app.constants with a user-extensible config object.
Related errors
- app.activeDocs is read-only
- app.formsVersion is read-only
- app.fromPDFConverters is read-only
- app.language is read-only
- app.monitors is read-only
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/a0ec352e07201958.
Report an issue: GitHub.