2

I am doing some code samples with node.js and yargs and I am trying to access yargs.command and yargs.version which I easily had access to with require.

But when I use import with the line:

import * as yargs from 'yargs';

It returns an error saying * is an invalid token

How can I successfully migrate to import?

tony_h
  • 95
  • 2
  • 10

2 Answers2

5

This is just to elaborate @Moreorem's answer (because I spent my sometime to properly figure it out :P)-

File - script.js

import Yargs from "yargs";
const args = Yargs(process.argv.slice(2)).argv;
console.log(args.arg1);

Command -

node script.js --arg1=xyz

Output -

xyz
Swaps
  • 1,450
  • 24
  • 31
1

Try import argv from 'yargs';

then you can use the arguments likewise node app.js --name Mike --port 22144

e.g. console.log(argv.name, argv.port) ...

Craftrac
  • 753
  • 1
  • 7
  • 13