Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 命令未使用yarg npm包添加_Node.js_Npm_Yargs - Fatal编程技术网

Node.js 命令未使用yarg npm包添加

Node.js 命令未使用yarg npm包添加,node.js,npm,yargs,Node.js,Npm,Yargs,我试图使用yarg添加命令,但当我运行代码时,没有添加命令 以下是我正在尝试的: const yargs = require('yargs') //create add command yargs.command({ command: 'add', describe: 'to add note', handler: function() { console.log('note has been added') } }) 运行命令: PS C:

我试图使用yarg添加命令,但当我运行代码时,没有添加命令

以下是我正在尝试的:

const yargs = require('yargs')

//create add command
yargs.command({
    command: 'add',
    describe: 'to add note',
    handler: function() {
        console.log('note has been added')
    } 
})
运行命令:

PS C:\Users\HP\Desktop\node\notes-app> node app.js --help
Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]
未添加添加命令

此外,当我尝试通过将add作为参数(即node app.js add)来运行代码时,不会显示任何内容


现在该怎么办?

根据
yargs
文档,方法
命令
需要4个参数,而不是一个对象

.command(cmd、desc、[builder]、[handler])

因此,您的代码应该类似(注意,不再有括号和对象键):

如果您没有传递可选的
builder
param,那么可能应该为handler函数使用named params(不确定
yargs
是如何命名params的,但我猜它是
handler
)类似的


根据
yargs
docs,方法
命令
取4个参数,不取对象

.command(cmd、desc、[builder]、[handler])

因此,您的代码应该类似(注意,不再有括号和对象键):

如果您没有传递可选的
builder
param,那么可能应该为handler函数使用named params(不确定
yargs
是如何命名params的,但我猜它是
handler
)类似的

如果不添加parse(),则不会执行yargs。如果你有太多的yargs命令类型

yargs.parse()

下面

如果不添加parse(),则不会执行yargs。如果你有太多的yargs命令类型

yargs.parse()

下面

yargs.command({
    command: 'add',
    describe: 'to add note',
    handler: function() {
        console.log('note has been added')
    } 
}).parse()
yargs.parse()
console.log(yargs.argv)