Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sub-commands do not work with package.json main #1044

Closed
samherrmann opened this issue Sep 14, 2019 · 3 comments
Closed

Sub-commands do not work with package.json main #1044

samherrmann opened this issue Sep 14, 2019 · 3 comments

Comments

@samherrmann
Copy link

samherrmann commented Sep 14, 2019

Consider the following setup:

lib/index.js

const program = require('commander');
 
program
  .command('update', 'update installed packages', { executableFile: './myUpdateSubCommand' })
  .parse(process.argv);

lib/myUpdateSubCommand.js

const program = require('commander');

program.command('foo').action(() => console.log('hello from foo'))
program.parse(process.argv);

if (program.args.length === 0) {
  program.help();
}

package.json

{
  "main": "lib/index.js"
}

What works:

$ node dist/index.js update
Usage: myUpdateSubCommand [options] [command]

Options:
  -h, --help  output usage information

Commands:
  foo

What does not work:

$ node . update
error: ./myUpdateSubCommand(1) does not exist, try --help

Expected Behavior

It's expected that executing node . update yields the same result as executing node dist/index.js update.

Where the problem is:

The path to the sub-command is determined from argv[1], which does not take into consideration the path assigned to the main property in package.json.

Version

"commander": "^3.0.1",

@shadowspawn
Copy link
Collaborator

Yes, for resolving subcommand executables Commander expects argv[1] is either the source file, or the installed binary command (i.e. a symbolic link). There is not currently support for coping with a package being passed to node (like when you call node .).

The approach I often use is a setup closer to the eventual install. So with a package.json like:

{
  "name": "my-command",
  "bin": "bin/index.js"
}

Instead of node . I use:

npm link
my-command update

(You could work-around the current design in your own code by supplying argv[1] yourself. I did a bit of reading and wonder how reliable using require.main.filename might be. )

@shadowspawn
Copy link
Collaborator

Related issue, when calling from Electron app: #512

@shadowspawn
Copy link
Collaborator

Closing as covered by #512

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants