Search code examples
node.jsyargs

I am trying to run yargs command, and it is not working


I am running:

node app.js add

And my code is:

const yargs = require('yargs');
yargs.command({
    command:'add',
    describe:'Adding command',
    handler:function(){
        console.log('Adding notes');
    },
})

But nothing printed on the console.


Solution

  • As @jonrsharpe mentioned in the comment above.

    You need to either call parse function or access argv property

    Try:

    const yargs = require('yargs');
    
    yargs
        .command({
            command:'add',
            describe:'Adding command',
            handler: argv => {
                console.log('Adding notes');
            }
        })
        .parse();
    

    Or

    const yargs = require('yargs');
    
    const argv = yargs
        .command({
            command: 'add',
            describe: 'Adding command',
            handler: argv => {
                console.log('Adding notes');
            }
        })
        .argv;
    

    node index.js add