在Node.js中使用导入时,Yargs不起作用
我是Node.js新手,现在正在学习一些基础知识。我试图稍后使用一些typescript代码转换成.js代码 我写了这个简单的代码来测试在Node.js中使用导入时,Yargs不起作用,node.js,typescript,yargs,Node.js,Typescript,Yargs,我是Node.js新手,现在正在学习一些基础知识。我试图稍后使用一些typescript代码转换成.js代码 我写了这个简单的代码来测试 import * as fs from 'fs' const argv = require('yargs') .alias('f', 'filename') .alias('c', 'content') .demandOption('filena
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.`)
})
这个很好用。但当我更改行时,需要('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库,在本例中,两种方法都可以正常工作。您需要从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
我认为ES6兼容导入仍然不受支持,只需要工作
import yargs from 'yargs'
console.log(yargs.argv)
$ node app.js
undefined
您是否尝试过通过运行以下命令来安装yargs打字机
npm安装--保存@types/yargs请参阅为什么不
console.log(yargs)
?对于没有外部依赖项或平台无法运行的脚本,请不要使用代码段。改为使用正确的代码格式[Ctrl+K]:单行程序使用单回号(“`”),属性名称和方法使用单回号(“`”),代码块使用代码围栏(“`”)。另外,请避免在问题中闲聊,因为这不是一个论坛,而是一个问答网站。