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

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

我正在尝试 运行 yargs.command 命令。我尝试了 运行 这段代码片段:

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");
  },
});

我在输出中得到了这个错误:

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)

在网上搜索后,我遇到了这个,并在我的代码末尾添加了这个语句:yargs.parse()。但不幸的是,我仍然得到相同的输出。

我的OS:MX-Linux21.

节点:12.22.5.

yargs: 17.4.1.

对比代码:1.66.2.

有人知道哪里出了问题吗?任何帮助将不胜感激。

yargs是returns你想要的.command属性对象的函数。来自自述文件:

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

您的代码应如下所示:

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

yargs(hideBin(process.argv))
    .command('list', 'List all commands', /*handler*/)
    .command(/*...*/)
    .parse();