Commander.js: TypeError: this.name is not a function

Created on 22 Mar 2020  路  4Comments  路  Source: tj/commander.js

Hey everyone,

i currently updated to commanderJs 5.0.0 and now some of my command are not working fine when providing the option name.

Given is the following command with the option name:
program.command('test').description('Logs something').option('-n, --name <value>').action(args => console.log(args.name))

If i run test the command works as expected.
But if i run test --name Foo in my console Commander throws the following error:
TypeError: this.name is not a function
at Command._parseCommand (....\node_modules\commander\index.js:910:37)
at Command._dispatchSubcommand (....\node_modules\commander\index.js:860:18)
at Command._parseCommand (....\node_modules\commander\index.js:877:12)
at Command.parse (....\node_modules\commander\index.js:712:10)

I already tried this Avoiding option name clashes

Anyone has any suggestions

Most helpful comment

name is a method on Command, so you will hit issues with an option --name when using default behaviour of storing the value as a property. It seems to work ok by adopting the new style processing for me?

const { Command } = require('commander');
const program = new Command();

program
  .storeOptionsAsProperties(false)
  .passCommandToAction(false)
  .name('my-name')
  .option('--name <value>', 'Enter name')
  .action((options) => {
    console.log(`Name option is ${options.name}`);
  });

program.parse();
$ node index.js           
Name option is undefined
$ node index.js --name foo
Name option is foo
$ node . --help
Usage: my-name [options]

Options:
  --name <value>  Enter name
  -h, --help      display help for command

All 4 comments

name is a method on Command, so you will hit issues with an option --name when using default behaviour of storing the value as a property. It seems to work ok by adopting the new style processing for me?

const { Command } = require('commander');
const program = new Command();

program
  .storeOptionsAsProperties(false)
  .passCommandToAction(false)
  .name('my-name')
  .option('--name <value>', 'Enter name')
  .action((options) => {
    console.log(`Name option is ${options.name}`);
  });

program.parse();
$ node index.js           
Name option is undefined
$ node index.js --name foo
Name option is foo
$ node . --help
Usage: my-name [options]

Options:
  --name <value>  Enter name
  -h, --help      display help for command

Your solution fixed the issue. Thanks for your answer.

Just ran into this one as well. Seems like it would be nice to throw an error if someone specifies a --name option. I can imagine this one being run into a fair bit, name is probably a pretty popular choice.

Opened a PR to add a warning for option name clashes: #1275

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hossamelmansy picture hossamelmansy  路  4Comments

shadowspawn picture shadowspawn  路  5Comments

mathiasbynens picture mathiasbynens  路  3Comments

snitin315 picture snitin315  路  4Comments

FerencH picture FerencH  路  4Comments