javascript - 命令未使用 yarg npm 包添加

标签 javascript 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:\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.command({
    command: 'add',
    describe: 'to add note',
    handler: function() {
        console.log('note has been added')
    } 
}).parse()

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

yargs.parse()

console.log(yargs.argv)

在下面。

关于javascript - 命令未使用 yarg npm 包添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55321403/

相关文章:

javascript - 动态 DFP 广告管理系统 JavaScript

javascript - 带有来自变量的 HTML 标记的 JSX

xml - node-soap 中是否有模式验证?

typescript - Npm 使用 vite React 应用程序运行预览可以工作,但我无法将其部署到 netlify

javascript - 类型错误 : Cannot read property 'id' of undefined when trying to make discord bot

javascript - 在js文件中嵌入laravel路由

node.js - 如何使用 PM2 运行多个 package.json 脚本之一?

javascript - NodeJS Async - 将参数传递给回调

node.js - npm 从 gitlab 安装模块

npm - 什么是 npm create 命令?