Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES6 IMPORT RESULTS UNDEFINED #1844

Open
salmansaleempiaic opened this issue Dec 30, 2020 · 4 comments
Open

ES6 IMPORT RESULTS UNDEFINED #1844

salmansaleempiaic opened this issue Dec 30, 2020 · 4 comments

Comments

@salmansaleempiaic
Copy link

Screen Shot 2020-12-30 at 5 26 57 AM

@bcoe
Copy link
Member

bcoe commented Jan 4, 2021

@salmansaleempiaic please provide code that I can copy and paste rather than a screen shot 👍 better still, could you provide a project I can clone on GitHub?

yargs does support ESM, and my guess is you have something configured slightly wrong in the project.

@thurinus
Copy link

I think ran into the same issue today while running through a tutorial.
package.json:

{
  "name": "playground",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "yargs": "^17.0.1"
  }
}

app.js:

import yargs from 'yargs';

console.log(process.argv);
console.log(yargs.argv);

running node .\app.js would yield:

[
  '<hidden dir>nodejs\\node.exe',
  '<hidden proj dir>\\app.js'
]
undefined

If I dropped the "type": "module" to go back to commonJS and used const yargs = require('yargs'), it works fine. Am I making a rookie mistake?

@aakasheoran
Copy link

It still has the same issue, can't use ES6 import syntax.

File Content:
import yargs from 'yargs';
console.log(yargs.Argv)

Output:
node .\app.js test1 test2
undefined

@bcoe
Copy link
Member

bcoe commented Dec 21, 2021

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)
  .parse()

☝️ if you're using pure ESM the format is as follows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants