Readme CJS example fails with versions of Node older than 12.17
See original GitHub issueUsing 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:
- Created 3 years ago
- Reactions:6
- Comments:13 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I researched a bit and found that
yargs/yargs
also exportshideBin
here, therefore following change fixes the problem.@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 additionalpackage.json
; It feels like the latest release of yargs solves this issue, and cleans up the directory structure a bit.