{"id":"760f5230b8a6fc3d","repo":"tj/commander.js","slug":"call-storeoptionsasproperties-before-adding-opt","errorCode":null,"errorMessage":"call .storeOptionsAsProperties() before adding options","messagePattern":"call \\.storeOptionsAsProperties\\(\\) before adding options","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/command.js","lineNumber":898,"sourceCode":"      !this.parent._enablePositionalOptions\n    ) {\n      throw new Error(\n        `passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`,\n      );\n    }\n  }\n\n  /**\n   * Whether to store option values as properties on command object,\n   * or store separately (specify false). In both cases the option values can be accessed using .opts().\n   *\n   * @param {boolean} [storeAsProperties=true]\n   * @return {Command} `this` command for chaining\n   */\n\n  storeOptionsAsProperties(storeAsProperties = true) {\n    if (this.options.length) {\n      throw new Error('call .storeOptionsAsProperties() before adding options');\n    }\n    if (Object.keys(this._optionValues).length) {\n      throw new Error(\n        'call .storeOptionsAsProperties() before setting option values',\n      );\n    }\n    this._storeOptionsAsProperties = !!storeAsProperties;\n    return this;\n  }\n\n  /**\n   * Retrieve option value.\n   *\n   * @param {string} key\n   * @return {object} value\n   */\n\n  getOptionValue(key) {","sourceCodeStart":880,"sourceCodeEnd":916,"githubUrl":"https://github.com/tj/commander.js/blob/ba6d13ddb4243e5913367734f8c159089ffe7834/lib/command.js#L880-L916","documentation":"Thrown by Command.storeOptionsAsProperties() at lib/command.js:897-899 when called after options have already been added. The storage mode (properties on `this` vs internal _optionValues map) affects how every option is registered and read, so it must be configured before any .option()/addOption() call.","triggerScenarios":"`program.option('-d','debug').storeOptionsAsProperties()` — options array is non-empty when the method runs, tripping the guard. Order-of-operations bug in setup code.","commonSituations":"Migrating from older Commander where storing as properties was the default; toggling storage mode mid-setup; calling storeOptionsAsProperties inside a factory that runs after options were declared elsewhere.","solutions":["Move the call to the very top of configuration, before any .option()/addOption(): `program.storeOptionsAsProperties(false).option(...)`.","Or stop using storeOptionsAsProperties entirely — modern Commander defaults to the _optionValues map and .opts() access, which is recommended."],"exampleFix":"// before (throws)\nprogram.option('-d, --debug', 'debug').storeOptionsAsProperties();\n\n// after\nprogram.storeOptionsAsProperties().option('-d, --debug', 'debug');","handlingStrategy":"validation","validationCode":"function configureStorage(cmd, asProperties) {\n  if (cmd.options.length) {\n    throw new Error('storeOptionsAsProperties must be called before adding options');\n  }\n  return cmd.storeOptionsAsProperties(asProperties);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Decide storage mode at the very top of setup, before any .option() call.","Prefer the default (false) and use .opts().","Encapsulate program construction in a factory so ordering is enforced."],"tags":["commander","option-storage","ordering","configuration"],"analyzedSha":"ba6d13ddb4243e5913367734f8c159089ffe7834","analyzedAt":"2026-08-03T20:26:04.326Z","schemaVersion":2}