1

所以,我正在做一个 nodejs 教程,但是该教程没有更新到 ES6,我只是想在进行过程中更新代码。我使用 require('yargs') 让程序运行得很好,但不断得到 yargs.command、yargs.argv 等不是函数。据我了解,nodejs 现在支持导入/导出语句,yargs 也是如此。我已将我的应用程序设置为“类型:模块”并保存为 .mjs 文件。下面列出的是带有终端输出的工作与非工作(ES6)。如果有人能发现我的错误,请告诉我...

工作代码...

// 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()

终端...

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

// 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()

终端....

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)
4

1 回答 1

1

我在github上发布了这个问题......这是解决方案https://github.com/yargs/yargs/issues/1854)。

这在高级主题 README.file ( https://github.com/yargs/yargs/blob/master/docs/advanced.md )中有解释

使用 index.mjs 的示例命令层次结构

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

cmds/index.mjs:

import * as a from './init.mjs'; import * as b from './remote.mjs';
export const commands = [a, b];

然后,该索引将被导入并在您的 CLI 中注册:

cli.js:

#!/usr/bin/env 节点

import yargs from 'yargs'; import { hideBin } from 'yargs/helpers';
import { commands } from './cmds/index.mjs';
 
yargs(hideBin(process.argv))
  .command(commands)
  .argv;

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

// 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)
  }
})

和终端输出...

~/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]
于 2021-03-07T21:21:13.653 回答