简体   繁体   English

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

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

I am trying to run the yargs.command command.我正在尝试运行yargs.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() .在互联网上搜索后,我遇到了这个解决方案,并在最后的代码中添加了这条语句: yargs.parse() But unfortunately, I am still getting the same output.但不幸的是,我仍然得到相同的输出。

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

Node: 12.22.5.节点:12.22.5。

yargs: 17.4.1.雅格斯:17.4.1。

vs code: 1.66.2.对比代码:1.66.2。

Does anyone have any clue what went wrong?有谁知道哪里出了问题? Any help would be appreciated.任何帮助,将不胜感激。

yargs is a function which returns the object with the .command property you want. yargs是一个函数,它返回具有所需.command属性的对象。 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();

This is wrong:这是错误的:

yargs.command({
  command:
})

The below is right:下面是对的:

yargs({
  command:
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM