Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facing this error: TypeError: yargs.command is not a function

Tags:

node.js

yargs

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.

like image 805
belmont Avatar asked Sep 16 '25 08:09

belmont


1 Answers

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();
like image 155
0xLogN Avatar answered Sep 18 '25 11:09

0xLogN