【问题标题】:Yargs not working when using import in Node.js在 Node.js 中使用导入时 Yargs 不起作用
【发布时间】:2020-02-04 22:41:56
【问题描述】:

我是 Node.js 的新手,现在正在学习一些基础知识。我正在尝试使用一些打字稿代码稍后转换为 .js 代码。

我写了这个简单的代码来测试

    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.`)
    })

这很好用。但是当我将 require('yargs') 行更改为导入时,如下所示:

   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.`)
    })

我收到此错误:

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)

有人知道使用模块/导入导致此错误有什么区别吗?对于 fs 库,在此示例中两种方式都可以正常工作。

【问题讨论】:

标签: node.js typescript yargs


【解决方案1】:

对于仍然想知道如何将 ES6 模块语法与 Yargs 一起使用的任何人,这里是更正的代码。我不得不使用 option() 添加一些类型信息以避免错误。更多信息请参考a Github discussion

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.`);
    });
})();

【讨论】:

    【解决方案2】:

    您需要从 argv 设置 args 的类型。尝试将您的核心更改为:

    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
    

    【讨论】:

    • 请不要将代码 sn-ps 用于没有外部依赖或平台无法运行的脚本。请改用正确的代码格式 [Ctrl+K]:单行反引号(“`”)、属性名称和方法、代码块的代码围栏(“````”)。另外请避免在问题中闲聊,SO不是论坛,而是问答网站。
    【解决方案3】:

    您是否尝试过运行以下命令来安装 yargs Typings?

    npm install --save @types/yargs

    【讨论】:

      【解决方案4】:

      我认为仍然不支持符合 ES6 标准的导入,只需要工作即可。

      import yargs from 'yargs'
      console.log(yargs.argv)
      
      $ node app.js
      undefined
      

      【讨论】:

      • 为什么不console.log(yargs)
      猜你喜欢
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      相关资源
      最近更新 更多