我正在尝试创建yargs命令,当我运行这个应用程序时,我会得到一个错误。
当我运行以下命令时:
node app.js add在我的node.js代码:
const yargs = require('yargs')
yargs.command({
command:'add',
describe:'Adding command',
handler:function(){
console.log('Adding notes')
}
}).parse()
console.log('yargs.argv')错误:
C:\node\notes-app\app.js:3
yargs.command({
^
**TypeError: yargs.command is not a function**
at Object.<anonymous> (C:\node\notes-app\app.js:3:7)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47version:1.1.0
编辑:(I have already viewed this question but it didn't help because already added .parse())
发布于 2021-12-20 12:20:49
试试看下面几行。
const yargs = require('yargs')
const {hideBin} = require('yargs/helpers')
yargs(hideBin(process.argv)).command({
command:'add',
describe:'Adding command',
handler:function(){
console.log('Adding notes')
}
}).parse()
console.log('yargs.argv')发布于 2021-05-21 02:17:25
纱1.1.0是很久以前的事了,其实已经7年了。那时他们没有.command()方法。
https://www.npmjs.com/package/yargs/v/1.1.0
最简单的解决方案就是更新。您应该能够使用节点v14.16至少转到v14或v15。
如果您不想这样做,则需要添加您自己的小型命令解析器:
const argv = require('yargs').argv;
if(argv._[0] === "add") {
// ...
} https://stackoverflow.com/questions/67418793
复制相似问题