面临这个错误:TypeError: yargs.command is not a function

lon*_*olf 4 node.js yargs

我正在尝试运行该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");
  },
});

Run Code Online (Sandbox Code Playgroud)

我在输出中收到此错误:

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)
Run Code Online (Sandbox Code Playgroud)

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

我的操作系统:MX-Linux 21。

节点:12.22.5。

亚格斯:17.4.1。

VS 代码:1.66.2。

有谁知道出了什么问题吗?任何帮助,将不胜感激。

Log*_*ine 9

yargs.command是一个返回具有所需属性的对象的函数。来自自述文件:

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

您的代码应该如下所示:

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

yargs(hideBin(process.argv))
    .command('list', 'List all commands', /*handler*/)
    .command(/*...*/)
    .parse();
Run Code Online (Sandbox Code Playgroud)