2

I am trying to create yargs command, when I run the app I get an error.

When I run the following:

node app.js add

on my node.js code:

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

console.log('yargs.argv')

Error:

C:\node\notes-app\app.js:3
yargs.command({
      ^
**TypeError: yargs.command is not a function**
    at Object.<anonymous> (C:\node\notes-app\app.js:3:7)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47

  • Yargs version:1.1.0
  • node version: v14.16.0
  • vs code version
  • 1.55.2(user setup) OS:Window 10

Edit: (I have already viewed this question but it didn't help because already added .parse())

0

2 Answers 2

2

Try following lines.

const yargs = require('yargs')
const {hideBin} = require('yargs/helpers')
yargs(hideBin(process.argv)).command({
    command:'add',
    describe:'Adding command',
    handler:function(){
        console.log('Adding notes')
    }
}).parse()
console.log('yargs.argv')
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

Yargs 1.1.0 is a long time ago, 7 years infact. Back then they didn't have the .command() method.

https://www.npmjs.com/package/yargs/v/1.1.0

The easiest solution is just to update. You should be able to go to atleast v14 or v15 with node v14.16.

If you don't want to you will need to add your own small command parser:

const argv = require('yargs').argv;

if(argv._[0] === "add") {
   // ...
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.