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.

Readme CJS example fails with versions of Node older than 12.17

See original GitHub issue

Using the CJS example code from the README.md seems to fail for me on all Node versions I tried before 12.17 (12.16.1, 12.15.0, 12.14.1 and 11.15.0):

#!/usr/bin/env node
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const argv = yargs(hideBin(process.argv)).argv

if (argv.ships > 3 && argv.distance < 53.5) {
  console.log('Plunder more riffiwobbles!')
} else {
  console.log('Retreat from the xupptumblers!')
}

The error varies by version.

12.16.1:

Error: Cannot find module 'yargs/helpers'

12.14.1 and 12.15.0:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: <snip>/yargs-node-error/node_modules/yargs/helpers.mjs

11.15.0:

Error: Cannot find module 'yargs/helpers'

Works fine in 12.17 and 12.18.

Here’s a sample of how I set it up: https://github.com/gpoole/yargs-node-error

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
apoorv-mishracommented, Oct 27, 2020

I researched a bit and found that yargs/yargs also exports hideBin here, therefore following change fixes the problem.

#!/usr/bin/env node
const yargs = require('yargs/yargs')
const { hideBin } = yargs
const argv = yargs(hideBin(process.argv)).argv

if (argv.ships > 3 && argv.distance < 53.5) {
  console.log('Plunder more riffiwobbles!')
} else {
  console.log('Retreat from the xupptumblers!')
}
3reactions
bcoecommented, Nov 15, 2020

@gpoole thank you for the bug report, this should be patched in the latest release now. @ljharb thanks for the idea of nesting inside helpers/ with an additional package.json; It feels like the latest release of yargs solves this issue, and cleans up the directory structure a bit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

was compiled against a different Node.js version using ...
I use NVM for versioning node and was running yarn via a terminal, but my IDE was set to use an older version...
Read more >
Learning by fixing: Node.js, modules and packages
Learn how Node resolves modules and deals with packages by pretending to be a detective and debugging a mysterious build failure.
Read more >
Troubleshooting - Puppeteer
This can occur is your Node.js version is lower than 14 or you are using a custom resolver (such as jest-resolve ). For...
Read more >
commander - npm
the complete solution for node.js command-line programs. Latest version: 9.4.1, ... This is used in the examples in this README for brevity.
Read more >
Version 2.0 released - Node-RED
The main focus of this release has been dropping support for old versions of Node.js that are themselves no longer supported.
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