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开发框架网发布内容,转载请附上原文出处连接
YES开发框架
上一篇:C#根据年份和第几个星期,获得这个星期的日期范围
下一篇:JSON.stringify 输出格式化文本
评论列表

发表评论

评论内容
昵称:
关联文章

nodeJS使用shelljs模块中文
ASP.NET Core 接口返回文件类型文件名中文
YESWEB业务模块指定系统表
模块维护
Python安装使用yaml模块
git仓库子模块(submodule)项目的实战应用
.net core MVC页面源文件文被编码
业务模块访问系统表
ABP vNext-模块
jQuery.cropper中文API详解
在ABP VNext框架对HttpApi模块的控制器进行基类封装
业务模块添加并引用静态资源
YESWin winform开发框架 新建一个业务模块项目
python安装pythoncom模块
C# ThoughtWorks.QRCode 二维生成和解析
Python使用UUID模块云服务器上获取MAC地址,重启后就不一样了
dotnetCampus.UITest.WPF 一个支持中文用例的界面单元测试框架
02、2G流量版收款播报机写
.NET Core使用编码GB2312报错‘GB2312‘ is not a supported encoding name 解决方案
.NET6一些常用组件的配置及使用记录,持续更新。。。

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码