question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting typeerror yargs.[methodname] is not a function using ES6 + node.js + yargs

See original GitHub issue

So, I’m doing a nodejs tutorial, but the tutorial isn’t updated to ES6 and I just wanted to update the code as I go along. I get the program to run just fine using require(‘yargs’), but keep getting yargs.command, yargs.argv, etc. is not a function. This happens with any and all yargs methods. I receive the error whether I try to use it in this example or any other. It’s my understanding that nodejs now supports import/export statements, as does yargs so I’m not using a bundler at all (I’m under the impression with ES6 supported in node and yargs I don’t need a bundler). I have set my app as “type: module” and also tried saving my app.js as an .mjs file. Listed below is the working vs. non-working(ES6) with terminal output. If it is a developer error I apologize for posting this as an issue, but please let me know…

Working code…

// ipmort module libraries from node package manager
const yargs = require('yargs')
const notes = require('./notes.js')

// Output colors
const chalk = require('chalk')
let jenkinsColor = '#bd93f9'
let successColor = '#50fa7b'
let failureColor = '#ff5555'

// Yargs stored version number
yargs.version('1.0.0')

// --- ADD COMMAND ----
yargs.command({
  command: 'add',
  describe: 'Have Jenkins add a new note',
  builder: {
    title: {
      describe: 'Note title',
      demandOption: true,
      type: 'string'
    },
    body: {
      describe: 'Note content',
      demandOption: true,
      type: 'string'
    }
  },
  handler(argv) {
    notes.addNote(argv.title, argv.body)
  }
})

// --- REMOVE COMMAND ----
yargs.command({
  command: 'remove',
  describe: 'Have Jenkins remove an existing note',
  builder: {
    title: {
      describe: 'Note to be deleted',
      demandOption: true,
      type: 'string'
    }
  },
  handler(argv) {
    notes.removeNote(argv.title)
  } 
})

// --- READ COMMAND ----
yargs.command({
  command: 'read',
  describe: 'Have Jenkins read your notes',
  handler() {
    console.log('Reading your notes, sir...')
  }
})

// --- LIST COMMAND ----
yargs.command({
  command: 'list',
  describe: 'Have Jenkins list your notes',
  handler() {
   console.log('Removing your note, sir...')
  }
})

yargs.parse()

Terminal output…

node-notes-app$ node app.js add --title="Test Title" --body="testing testing 123"

      ...............................
      + Test Title
      ...............................
      + + testing testing 123
    
SUCCESS
Adding your new note to your list, sir...
christopher@rra-debian-desktop:~/Documents/IBM/FED/NodeJS/node-notes-app$ 

ES6 - Not working

// import module libraries from node package manager
import yargs from 'yargs'
// import notes from './notes.js'

// Yargs stored version number
yargs.version('1.0.0')

// --- ADD COMMAND ----
yargs.command({
  command: 'add',
  describe: 'Have Jenkins add a new note',
  builder: {
    title: {
      describe: 'Note title',
      demandOption: true,
      type: 'string'
    },
    body: {
      describe: 'Note content',
      demandOption: true,
      type: 'string'
    }
  },
  handler(argv) {
    notes.addNote(argv.title, argv.body)
  }
})

// --- REMOVE COMMAND ----
yargs.command({
  command: 'remove',
  describe: 'Have Jenkins remove an existing note',
  builder: {
    title: {
      describe: 'Note to be deleted',
      demandOption: true,
      type: 'string'
    }
  },
  handler(argv) {
    notes.removeNote(argv.title)
  } 
})

// --- READ COMMAND ----
yargs.command({
  command: 'read',
  describe: 'Have Jenkins read your notes',
  handler() {
    console.log('Reading your notes, sir...')
  }
})

// --- LIST COMMAND ----
yargs.command({
  command: 'list',
  describe: 'Have Jenkins list your notes',
  handler() {
   console.log('Removing your note, sir...')
  }
})

yargs.parse()

ES6 Terminal output…

app.mjs:12
yargs.version('1.0.0')
      ^

TypeError: yargs.version is not a function
    at file:///home/christopher/Documents/IBM/FED/NodeJS/node-notes-app/app.mjs:12:7
    at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
    at async Loader.import (internal/modules/esm/loader.js:166:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

29reactions
bcoecommented, Feb 28, 2021

@davidfilat @ctcoleman in ES6, yargs is no longer a singleton, so you need to call yargs() to get an instance of yargs, at which point the API is identical, here’s a simple example:

import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'

yargs(hideBin(process.argv))
  .command('curl <url>', 'fetch the contents of the URL', () => {}, (argv) => {
    console.info(argv)
  })
  .demandCommand(1)
  .argv

Here’s your example rewritten:

// import module libraries from node package manager
import yargs from 'yargs'
// import notes from './notes.js'

// Yargs stored version number
const y = yargs()
y.version('1.0.0')

// --- ADD COMMAND ----
y.command({
  command: 'add',
  describe: 'Have Jenkins add a new note',
  builder: {
    title: {
      describe: 'Note title',
      demandOption: true,
      type: 'string'
    },
    body: {
      describe: 'Note content',
      demandOption: true,
      type: 'string'
    }
  },
  handler(argv) {
    notes.addNote(argv.title, argv.body)
  }
})

// --- REMOVE COMMAND ----
y.command({
  command: 'remove',
  describe: 'Have Jenkins remove an existing note',
  builder: {
    title: {
      describe: 'Note to be deleted',
      demandOption: true,
      type: 'string'
    }
  },
  handler(argv) {
    notes.removeNote(argv.title)
  } 
})

// --- READ COMMAND ----
y.command({
  command: 'read',
  describe: 'Have Jenkins read your notes',
  handler() {
    console.log('Reading your notes, sir...')
  }
})

// --- LIST COMMAND ----
y.command({
  command: 'list',
  describe: 'Have Jenkins list your notes',
  handler() {
   console.log('Removing your note, sir...')
  }
})

y.parse(process.argv.slice(2))

@SimoneGianniHNP I don’t have a clue what’s happening to you, and can’t reproduce locally. Could you create an example repository that demonstrates this behavior? (I think it might actually be an issue with your local configuration).

8reactions
ctcolemancommented, Mar 7, 2021

Worked like a charm…

Working reformatted code…

import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import notes from './notes.mjs'

const yarg = yargs(hideBin(process.argv))

// Yargs stored version number
yarg.version('1.0.0')

// --- ADD COMMAND ----
yarg.command({
  command: 'add',
  describe: 'Have Jenkins add a new note',
  builder: {
    title: {
      describe: 'Note title',
      demandOption: true,
      type: 'string'
    },
    body: {
      describe: 'Note content',
      demandOption: true,
      type: 'string'
    }
  },
  handler(argv) {
    notes.addNote(argv.title, argv.body)
  }
})

Terminal output…

~/Projects/IBM/BED/NodeJS/node-notes-app$ node app.mjs --help
app.mjs [command]

Commands:
  app.mjs add     Have Jenkins add a new note
  app.mjs remove  Have Jenkins remove an existing note
  app.mjs read    Have Jenkins read your notes
  app.mjs list    Have Jenkins list your notes

Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]

Thanks again@bcoe

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - I'm getting yargs.methodName is not a function with ...
node. js - I'm getting yargs. methodName is not a function with all yargs methods using ES6 imports in NodeJS without Babel -...
Read more >
Additional documentation - yargs - JS.ORG
This function is executed with a yargs instance, which can be used to provide command-specific configuration, and the boolean helpOrVersionSet , which indicates ......
Read more >
Node.js Yargs Module - GeeksforGeeks
Yargs module is used for creating your own command-line commands in node.js and helps in generating an elegant user interface.
Read more >
yargs - npm
Yargs helps you build interactive command line tools, by parsing arguments and generating an elegant user interface. It gives you: commands and ...
Read more >
Read more - El grupo SING
The server lists it's +preferences in order and will get back the best match between ... EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found