babel/babel · error · Error
The `annexB` option can only be set to `false`.
Error message
The `annexB` option can only be set to `false`.
What it means
This Error is thrown by getOptions() in @babel/parser when normalizing options. The `annexB` option defaults to true (Annex B web-browser syntax is on); Babel only allows opting out, so any value that is not exactly `false` (true, truthy strings, numbers, objects) is rejected with 'The `annexB` option can only be set to `false`.' It prevents ambiguous enablement of an already-default feature.
Source
Thrown at packages/babel-parser/src/options.ts:259
// When enabled, the parser will support Annex B syntax.
// https://tc39.es/ecma262/#sec-additional-ecmascript-features-for-web-browsers
annexB: true,
};
}
// Interpret and default an options object
export function getOptions(opts?: Options | null): OptionsWithDefaults {
// https://github.com/babel/babel/pull/16918
// `options` is accessed frequently, please make sure it is a fast object.
// `%ToFastProperties` can make it a fast object, but the performance is the same as the slow object.
const options: any = createDefaultOptions();
if (opts == null) {
return options;
}
if (opts.annexB != null && opts.annexB !== false) {
throw new Error("The `annexB` option can only be set to `false`.");
}
for (const key of Object.keys(options) as (keyof Options)[]) {
if (opts[key] != null) options[key] = opts[key];
}
if (options.startLine === 1) {
if (opts.startIndex == null && options.startColumn > 0) {
options.startIndex = options.startColumn;
} else if (opts.startColumn == null && options.startIndex > 0) {
options.startColumn = options.startIndex;
}
} else if (opts.startColumn == null || opts.startIndex == null) {
throw new Error(
"With a `startLine > 1` you must also specify `startIndex` and `startColumn`.",
);
}
View on GitHub (pinned to 06b6eae39d)
Solutions
- Omit annexB entirely (defaults to true), or set it to `false` to disable Annex B syntax.
- Sanitize boolean options to undefined when they should use the default.
- Fix the option producer so it only emits annexB when the user explicitly disabled it.
Example fix
// before
parse(code, { annexB: true }); // throws
// after
parse(code, {}); // annexB defaults to true
// or, to disable Annex B:
parse(code, { annexB: false }); Defensive patterns
Strategy: validation
Validate before calling
const opts = { ...userOpts };
if ('annexB' in opts && opts.annexB !== false) {
delete opts.annexB; // rely on default (true)
}
parse(code, opts); Type guard
const isAnnexBOption = (v: unknown): boolean => v === undefined || v === false;
Prevention
- Only set annexB to false (to disable) or omit it (defaults true).
- Do not blanket-set boolean parser options to true.
- Validate generated option objects before passing to parse().
When it happens
Trigger: Passing `{ annexB: true }`, `{ annexB: 'yes' }`, `{ annexB: 1 }`, or `{ annexB: {} }` to parse()/parseExpression() options. The check is `opts.annexB != null && opts.annexB !== false`.
Common situations: Generated config that sets every boolean option to true; migrating from a tool that accepted annexB as a string; copying options objects and flipping flags wholesale; a wrapper that defaults missing booleans to true.
Related errors
- With a `startLine > 1` you must also specify `startIndex` an
- The `allowAwaitOutsideFunction` option cannot be used with `
- `sourceType: 'commonjs'` implies `allowReturnOutsideFunction
- ${msg(access(loc, 1))} must be an object, false, or undefine
- ${descriptor}: '${option}' is not a valid top-level option.
AI-assisted analysis of babel/babel@06b6eae39d (2026-08-03).
Data as JSON: /data/errors/5bbc8846ef75902c.json.
Report an issue: GitHub.