{"id":"f7ca26ad998e1b7a","repo":"tj/commander.js","slug":"command-alias-can-t-be-the-same-as-its-name","errorCode":null,"errorMessage":"Command alias can't be the same as its name","messagePattern":"Command alias can't be the same as its name","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/command.js","lineNumber":2278,"sourceCode":"   * @return {(string|Command)}\n   */\n\n  alias(alias) {\n    if (alias === undefined) return this._aliases[0]; // just return first, for backwards compatibility\n\n    /** @type {Command} */\n    // eslint-disable-next-line @typescript-eslint/no-this-alias\n    let command = this;\n    if (\n      this.commands.length !== 0 &&\n      this.commands[this.commands.length - 1]._executableHandler\n    ) {\n      // assume adding alias for last added executable subcommand, rather than this\n      command = this.commands[this.commands.length - 1];\n    }\n\n    if (alias === command._name)\n      throw new Error(\"Command alias can't be the same as its name\");\n    const matchingCommand = this.parent?._findCommand(alias);\n    if (matchingCommand) {\n      // c.f. _registerCommand\n      const existingCmd = [matchingCommand.name()]\n        .concat(matchingCommand.aliases())\n        .join('|');\n      throw new Error(\n        `cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`,\n      );\n    }\n\n    command._aliases.push(alias);\n    return this;\n  }\n\n  /**\n   * Set aliases for the command.\n   *","sourceCodeStart":2260,"sourceCodeEnd":2296,"githubUrl":"https://github.com/tj/commander.js/blob/ba6d13ddb4243e5913367734f8c159089ffe7834/lib/command.js#L2260-L2296","documentation":"Thrown by Command.alias() at lib/command.js:2277-2278 when the alias being added equals the command's own `_name`. An alias identical to the name is redundant and would confuse dispatch/help/conflict detection (knownBy would list the same string twice). Note the method supports an indirection: if the last registered command is an executable subcommand, the alias applies to it, not to `this`.","triggerScenarios":"`new Command('build').alias('build')`, or dynamic `cmd.alias(cmd.name())`. Also when an alias is computed from config and happens to equal the name.","commonSituations":"Generating aliases from a list that includes the command's own name; copy-paste where alias mirrors name; refactor that renamed a command but left an alias equal to the old name which is now the new name.","solutions":["Pick an alias different from the name: `cmd.alias('b')` for a 'build' command.","If you don't actually need an alias, remove the .alias() call.","When generating aliases programmatically, filter out the command name: `names.filter(n => n !== cmd.name()).forEach(n => cmd.alias(n))`."],"exampleFix":"// before (throws)\nprogram.command('build').alias('build');\n\n// after\nprogram.command('build').alias('b');","handlingStrategy":"validation","validationCode":"function safeAlias(cmd, alias) {\n  if (alias === cmd.name()) {\n    throw new Error(`alias '${alias}' must differ from command name`);\n  }\n  return cmd.alias(alias);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Filter the command name out of any auto-generated alias list.","Add a unit test asserting every alias differs from its command's name.","Review aliases whenever renaming a command."],"tags":["commander","alias","configuration","validation"],"analyzedSha":"ba6d13ddb4243e5913367734f8c159089ffe7834","analyzedAt":"2026-08-03T20:26:04.326Z","schemaVersion":2}