tj/commander.js · error · Error

call .storeOptionsAsProperties() before setting option value

Error message

call .storeOptionsAsProperties() before setting option values

What it means

Thrown by Command.storeOptionsAsProperties() at lib/command.js:900-904 when option values have already been set (Object.keys(this._optionValues).length > 0) at the time the method is called. Switching storage mode after values exist would orphan them, so Commander refuses.

Source

Thrown at lib/command.js:901

        `passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`,
      );
    }
  }

  /**
   * Whether to store option values as properties on command object,
   * or store separately (specify false). In both cases the option values can be accessed using .opts().
   *
   * @param {boolean} [storeAsProperties=true]
   * @return {Command} `this` command for chaining
   */

  storeOptionsAsProperties(storeAsProperties = true) {
    if (this.options.length) {
      throw new Error('call .storeOptionsAsProperties() before adding options');
    }
    if (Object.keys(this._optionValues).length) {
      throw new Error(
        'call .storeOptionsAsProperties() before setting option values',
      );
    }
    this._storeOptionsAsProperties = !!storeAsProperties;
    return this;
  }

  /**
   * Retrieve option value.
   *
   * @param {string} key
   * @return {object} value
   */

  getOptionValue(key) {
    if (this._storeOptionsAsProperties) {
      return this[key];
    }

View on GitHub (pinned to ba6d13ddb4)

Solutions

  1. Set storage mode at construction time, before parse and before setting any values.
  2. Use a fresh Command instance per parse if you need different modes (also required because storeOptionsAsProperties=true disallows re-parse — see error[15]).
  3. Prefer the modern default (storeOptionsAsProperties(false)) and access via .opts().

Example fix

// before (throws)
program.parse(argv);
program.storeOptionsAsProperties();

// after
program.storeOptionsAsProperties(); // before any parse
program.parse(argv);
Defensive patterns

Strategy: validation

Validate before calling

function configureStorage(cmd, asProperties) {
  if (Object.keys(cmd._optionValues ?? {}).length) {
    throw new Error('storage mode set after option values exist');
  }
  return cmd.storeOptionsAsProperties(asProperties);
}

Prevention

When it happens

Trigger: Calling storeOptionsAsProperties after program.parse() has run (parse populates _optionValues), or after manually setting a default. Less common than error[11] but same root: storage mode must be decided before any value exists.

Common situations: Late initialization in a test that already parsed once; reusing a Command instance across parses with a mode switch in between; setting option defaults imperatively before declaring storage mode.

Related errors


AI-assisted analysis of tj/commander.js@ba6d13ddb4 (2026-08-03). Data as JSON: /data/errors/bd153fea1f8e0c4a.json. Report an issue: GitHub.