【问题标题】:Migrate from require to import es6 with yargs从 require 迁移到使用 yargs 导入 es6
【发布时间】:2019-11-07 15:58:02
【问题描述】:

我正在使用 node.js 和 yargs 做一些代码示例,我正在尝试访问 yargs.command 和 yargs.version,我可以通过 require 轻松访问它们。

但是当我使用 import 时:

import * as yargs from 'yargs';

它返回一个错误,说 * 是一个无效的令牌

如何成功迁移到导入?

【问题讨论】:

    标签: node.js yargs


    【解决方案1】:

    这只是为了详细说明@Moreoremanswer(因为我花了一些时间来正确弄清楚:P)-

    文件 - script.js

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

    命令 -

    node script.js --arg1=xyz
    

    输出 -

    xyz
    

    【讨论】:

      【解决方案2】:

      试试import argv from 'yargs';

      那么你可以同样使用参数 node app.js --name Mike --port 22144

      例如console.log(argv.name, argv.port) ...

      【讨论】:

        猜你喜欢
        • 2019-02-24
        • 2015-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-24
        • 2017-02-13
        • 2019-06-06
        • 2021-06-11
        相关资源
        最近更新 更多