Yargs not working when using import in Node.js

Viewed 3885

I'm new to Node.js and I'm learning some basics now. I'm trying to use some typescript code to convert into .js code later.

I wrote this simple code to test

    import * as fs from 'fs'


    const argv = require('yargs')
                .alias('f', 'filename')
                .alias('c', 'content')
                .demandOption('filename')
                .demandOption('content')
                .argv

    fs.writeFile(argv.filename, argv.content, (error)=>{
        if(error) 
            throw error
        console.log(`File ${argv.filename} saved.`)
    })

And this works fine. But when I change the line require('yargs') to import, like below:

   import * as fs from 'fs'
   import * as yargs from 'yargs'

    const argv = yargs
                .alias('f', 'filename')
                .alias('c', 'content')
                .demandOption('filename')
                .demandOption('content')
                .argv

    fs.writeFile(argv.filename, argv.content, (error)=>{
        if(error) 
            throw error
        console.log(`File ${argv.filename} saved.`)
    })

I'm getting this error:

Argument of type 'unknown' is not assignable to parameter of type 'string | number | Buffer | URL'.

Type '{}' is missing the following properties from type 'URL': hash, host, hostname, href, and 9 more.ts(2345)

Does anybody know what's the difference between using module/import that is causing this error? For fs library both ways work fine in this example.

4 Answers

Have you tried installing yargs typings, by running the following command?

npm install --save @types/yargs

Here is the corrected code for anyone still wondering how to use ES6 module syntax with Yargs. I had to add some type information with option() to avoid errors. Refer to a Github discussion for more information.

import fs from 'fs';
import _yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
const yargs = _yargs(hideBin(process.argv));

(async () => {
    const argv = await yargs
        .option('filename', { type: 'string', require: true })
        .option('content', { type: 'string', require: true })
        .alias('f', 'filename')
        .alias('c', 'content')
        .argv;

    fs.writeFile(argv.filename, '' + argv.content, error => {
        if (error) throw error;
        console.log(`File ${argv.filename} saved.`);
    });
})();

I think ES6 compliant import is still not supported and only require works.

import yargs from 'yargs'
console.log(yargs.argv)

$ node app.js
undefined

You need to set the type of args from argv. Try to change your core to:

const argv = yargs
        .option('filename', {
            alias: 'f',
            demandOption: true,
            describe: 'Nome do arquivo',
            type: 'string'
        })
        .option('content', {
            alias: 'c',
            demandOption: true,
            describe: 'Conteudo',
            type: 'string'
        })
        .argv
Related