4

I am trying to run the yargs.command command. I tried running this code snippet :

import yargs from "yargs";
yargs.command({
  command: "list",
  describe: "List all commands",
  handler: function () {
    console.log("Listing all commands.");
  },
});

yargs.command({
  command: "read",
  describe: "Reading all commands",
  handler: function () {
    console.log("Reading all commands");
  },
});

And I got this error in the output:

TypeError: yargs.command is not a function
    at file:///media/Main-Volume/courses/Udemy%20courses/Node%20JS%20bootcamp/notes-app/app.js:23:7
    at ModuleJob.run (internal/modules/esm/module_job.js:145:37)
    at async Loader.import (internal/modules/esm/loader.js:182:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)

After searching on the internet, I came across this solution and added this statement in my code at the end: yargs.parse(). But unfortunately, I am still getting the same output.

My OS: MX-Linux 21.

Node: 12.22.5.

yargs: 17.4.1.

vs code: 1.66.2.

Does anyone have any clue what went wrong? Any help would be appreciated.

1
  • This will help you Click here. Commented Feb 13 at 9:42

2 Answers 2

9

yargs is a function which returns the object with the .command property you want. From the README:

yargs(hideBin(process.argv))
  .command('curl <url>', 'fetch the contents of the URL', () => {}, (argv) => {
    console.info(argv)
  })
  .demandCommand(1)
  .parse()

Your code should look something like this:

import yargs from "yargs";
import {hideBin} from "yargs/helpers";

yargs(hideBin(process.argv))
    .command('list', 'List all commands', /*handler*/)
    .command(/*...*/)
    .parse();
Sign up to request clarification or add additional context in comments.

3 Comments

So what should the code look like? I am not getting at all what you are trying to say. My apologies for that.
@lonewolf Is that more clear? Also help is the more common name, not 'list'
it is solved. Thanks. I was doing that as part of an exercise in an online course. I was asked to include 'list' instead of 'help.'
-3

This is wrong:

yargs.command({
  command:
})

The below is right:

yargs({
  command:
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.