如何美化/美化node.js脚本中的Json/JS文件 [英] How to beautify/prettify a Json/JS file in a node.js script

查看:336
本文介绍了如何美化/美化node.js脚本中的Json/JS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在node.js脚本(不是CLI)中美化Json文件的方法.我找到了很多npm美化程序包,但是没有一个可以直接美化文件.

I am searching a way to prettify Json files in a node.js script (not CLI). I found a lot of npm beautifier packages, but none that can simply beautify directly a file.

有一个esbeautifier可以执行我要搜索的操作,但是示例仅显示CLI命令: https://github.com/royriojas/esbeautifier 有没有办法在Javascript中使用它?

There is esbeautifier that do what I am searching to do, but the exemples only shows CLI commands: https://github.com/royriojas/esbeautifier Is there a way to use it in a Javascript?

推荐答案

您可以使用工具 esformatter

通过@jck这是使用fs的JS代码段:

edit by @jck: here is JS snippet that works using fs:

var esformatter = require('esformatter');
var fs = require('fs');
var filename = "./myFile.json";
var codeStr = fs.readFileSync(filename).toString();
var formattedCode = esformatter.format(codeStr);
fs.writeFile(filename, formattedCode);

这篇关于如何美化/美化node.js脚本中的Json/JS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆