Search code examples
javascriptnode.jsnpmyargs

command is not adding using yarg npm package


I am trying to add a command using yarg, but when I run my code my command is not added.

Here is what I am trying:

const yargs = require('yargs')

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

run command:

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

No add command is added.

Also, when I try to run my code by giving add as an argument (i.e., node app.js add) nothing is shown.

What should I do now?


Solution

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

    if u dont add parse(), yargs will not execute. if u have too many yargs commands type

    yargs.parse()
    

    or

    console.log(yargs.argv)
    

    down below.