nodeJS中使用shelljs模块中文乱码
nodejs中使用shelljs执行cmd指令,输出结果有中文乱码
shell.exec(`move ${curName} ${tarName}`)
方案一:
参考:https://github.com/shelljs/shelljs/issues/456
const iconv = require('iconv-lite')
shell.exec(
`move ${curName} ${tarName}`,
{ encoding: 'base64' },
(error, stdout, stderr) => {
if (error) {
console.error(
`exec error:${iconv.decode(
iconv.encode(error, 'base64'),
'gb2312'
)}`
)
return
}
console.log(
`${iconv.decode(iconv.encode(stdout, 'base64'), 'gb2312')}`
)
console.log(
`${iconv.decode(iconv.encode(stderr, 'base64'), 'gb2312')}`
)
}
)
执行结果如图所示:
红框圈起来的地方多出来的base64 不是很完美
方案二 (推荐)
参考:https://blog.csdn.net/quzhongxin/article/details/45336333
执行脚本前先执行 命令 chcp 65001
shell.exec(`chcp 65001`);
shell.exec(`git clone ${remote} --depth=1`)
shell.exec(`move ${curName} ${tarName}`)
执行结果:
解决了
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
post YES开发框架