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

You have to provide the yargs.parse(); or yargs.argv; after defining all the commands.

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

yargs.parse();
//or
yargs.argv;

or

You can .argv or .parse() specify individually

    yargs.command({
        command:'add',
        describe:'Adding command',
        handler:function(){
            console.log('Adding notes');
        },
    }).parse() or .argv;

This is okay if you have one command. But for multiple commands, do yargs.argv at last after defining all commands.

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

Example solution:

const yargs = require('yargs')

//add command
yargs.command({
    command: 'add',
    describe: 'Add a new note',
    handler: ()=>{
        console.log("Adding a new note")
    }
})
//remove Command
yargs.command({
    command: 'remove',
    describe: "Remove a Note",
    handler: ()=>{
        console.log("removing note")
    }
})

yargs.parse()

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

Tags:

Node.Js

Yargs