首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我得到的yargs.methodName并不是所有yargs方法的函数在没有Babel的情况下在NodeJS中使用ES6导入

我得到的yargs.methodName并不是所有yargs方法的函数在没有Babel的情况下在NodeJS中使用ES6导入
EN

Stack Overflow用户
提问于 2021-01-14 09:34:52
回答 1查看 577关注 0票数 1

所以,我正在做一个nodejs教程,但是教程没有更新到ES6,我只是想在学习的过程中更新代码。我使用require('yargs')让程序运行得很好,但是不断得到yargs.command、yargs.argv等不是一个函数。据我所知,nodejs现在支持import/export语句,yargs也是如此。我已经将我的应用程序设置为"type: module“并另存为.mjs文件。下面列出了具有终端输出的工作和非工作(ES6)。如果有人能发现我的错误,请让我知道…

工作代码..。

代码语言:javascript
复制
// ipmort module libraries from node package manager
const yargs = require('yargs')
const notes = require('./notes.js')

// Output colors
const chalk = require('chalk')
let jenkinsColor = '#bd93f9'
let successColor = '#50fa7b'
let failureColor = '#ff5555'

// Yargs stored version number
yargs.version('1.0.0')

// --- ADD COMMAND ----
yargs.command({
  command: 'add',
  describe: 'Have Jenkins add a new note',
  builder: {
    title: {
      describe: 'Note title',
      demandOption: true,
      type: 'string'
    },
    body: {
      describe: 'Note content',
      demandOption: true,
      type: 'string'
    }
  },
  handler(argv) {
    notes.addNote(argv.title, argv.body)
  }
})

// --- REMOVE COMMAND ----
yargs.command({
  command: 'remove',
  describe: 'Have Jenkins remove an existing note',
  builder: {
    title: {
      describe: 'Note to be deleted',
      demandOption: true,
      type: 'string'
    }
  },
  handler(argv) {
    notes.removeNote(argv.title)
  } 
})

// --- READ COMMAND ----
yargs.command({
  command: 'read',
  describe: 'Have Jenkins read your notes',
  handler() {
    console.log('Reading your notes, sir...')
  }
})

// --- LIST COMMAND ----
yargs.command({
  command: 'list',
  describe: 'Have Jenkins list your notes',
  handler() {
   console.log('Removing your note, sir...')
  }
})

yargs.parse()

终端..。

代码语言:javascript
复制
node-notes-app$ node app.js add --title="Test Title" --body="testing testing 123"

      ...............................
      + Test Title
      ...............................
      + + testing testing 123
    
SUCCESS
Adding your new note to your list, sir...
christopher@rra-debian-desktop:~/Documents/IBM/FED/NodeJS/node-notes-app$ 

ES6

代码语言:javascript
复制
// import module libraries from node package manager
import yargs from 'yargs'
// import notes from './notes.js'

// Yargs stored version number
yargs.version('1.0.0')

// --- ADD COMMAND ----
yargs.command({
  command: 'add',
  describe: 'Have Jenkins add a new note',
  builder: {
    title: {
      describe: 'Note title',
      demandOption: true,
      type: 'string'
    },
    body: {
      describe: 'Note content',
      demandOption: true,
      type: 'string'
    }
  },
  handler(argv) {
    notes.addNote(argv.title, argv.body)
  }
})

// --- REMOVE COMMAND ----
yargs.command({
  command: 'remove',
  describe: 'Have Jenkins remove an existing note',
  builder: {
    title: {
      describe: 'Note to be deleted',
      demandOption: true,
      type: 'string'
    }
  },
  handler(argv) {
    notes.removeNote(argv.title)
  } 
})

// --- READ COMMAND ----
yargs.command({
  command: 'read',
  describe: 'Have Jenkins read your notes',
  handler() {
    console.log('Reading your notes, sir...')
  }
})

// --- LIST COMMAND ----
yargs.command({
  command: 'list',
  describe: 'Have Jenkins list your notes',
  handler() {
   console.log('Removing your note, sir...')
  }
})

yargs.parse()

终点站...

代码语言:javascript
复制
app.mjs:12
yargs.version('1.0.0')
      ^

TypeError: yargs.version is not a function
    at file:///home/christopher/Documents/IBM/FED/NodeJS/node-notes-app/app.mjs:12:7
    at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
    at async Loader.import (internal/modules/esm/loader.js:166:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)
EN

回答 1

Stack Overflow用户

发布于 2021-03-08 05:21:14

我在github...here is the solution (https://github.com/yargs/yargs/issues/1854)上发布了这期文章。

这在Advanced Topics README.file (https://github.com/yargs/yargs/blob/master/docs/advanced.md)中有解释

使用index.mjs的

命令层次结构示例

为了支持在使用ESM时创建复杂的嵌套CLI,扩展了方法.command()以接受命令模块数组。不使用.commandDir(),而是使用命令列表在每个命令目录中创建一个index.mjs:

cmds/index.mjs:

代码语言:javascript
复制
import * as a from './init.mjs'; import * as b from './remote.mjs';
export const commands = [a, b];

然后将导入此索引并将其注册到您的命令行界面:

cli.js:

#!/usr/bin/env节点

代码语言:javascript
复制
import yargs from 'yargs'; import { hideBin } from 'yargs/helpers';
import { commands } from './cmds/index.mjs';
 
yargs(hideBin(process.argv))
  .command(commands)
  .argv;

下面是我重新格式化的工作代码app.mjs的一部分

代码语言:javascript
复制
// import module libraries from node package manager
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import notes from './notes.mjs'

const yarg = yargs(hideBin(process.argv))

// Yargs stored version number
yarg.version('1.0.0')

// --- ADD COMMAND ----
yarg.command({
  command: 'add',
  describe: 'Have Jenkins add a new note',
  builder: {
    title: {
      describe: 'Note title',
      demandOption: true,
      type: 'string'
    },
    body: {
      describe: 'Note content',
      demandOption: true,
      type: 'string'
    }
  },
  handler(argv) {
    notes.addNote(argv.title, argv.body)
  }
})

终端输出..。

代码语言:javascript
复制
~/Projects/IBM/BED/NodeJS/node-notes-app$ node app.mjs --help
app.mjs [command]

Commands:
  app.mjs add     Have Jenkins add a new note
  app.mjs remove  Have Jenkins remove an existing note
  app.mjs read    Have Jenkins read your notes
  app.mjs list    Have Jenkins list your notes

Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65712177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档