Reputation: 4038
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.
Upvotes: 11
Views: 13606
Reputation: 1
I also face this issue and find the solution with latest update of node. You need to change file extension from .js to .cjs and its working fine.
Upvotes: 0
Reputation: 23
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()
Upvotes: 2
Reputation: 351
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;
Upvotes: 10
Reputation: 6068
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
Upvotes: 20