You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用js-beautify库时出现JavaScript未捕获引用错误的原因排查

解决js-beautify的Uncaught ReferenceError问题

你遇到的Uncaught ReferenceError是因为调用了错误的全局函数名。当引入独立的beautify-html.js、beautify.js、beautify-css.js文件后,不同类型的格式化对应不同的全局函数:

  • 格式化HTML:使用html_beautify
  • 格式化JavaScript:使用js_beautify
  • 格式化CSS:使用css_beautify

修正后的测试代码如下:

window.addEventListener('load', (e) => {
  console.log("test");
  // 调用正确的HTML格式化函数
  const formattedHtml = html_beautify("<html></html>", { indent_size: 2, space_in_empty_paren: true });
  console.log(formattedHtml); // 输出格式化后的HTML内容
});

如果需要格式化JS代码,示例如下:

const formattedJs = js_beautify("function test(){console.log('hello')}", { indent_size: 2 });

内容的提问来源于stack exchange,提问作者user310291

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 10:01:57