gpt4 book ai didi

node.js - 面对这个错误 : TypeError: yargs. 命令不是函数

转载 作者:行者123 更新时间:2023-12-05 05:43:20 28 4
gpt4 key购买 nike

我正在尝试运行 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)

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

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

Node :12.22.5.

yargs:17.4.1.

对比代码:1.66.2。

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

最佳答案

yargs 是一个函数,它返回具有您想要的 .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();

关于node.js - 面对这个错误 : TypeError: yargs. 命令不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71866261/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com