javascript - html-minifier node.js TypeError : value. 替换不是函数

标签 javascript node.js minify

我正在尝试使用 Node 模块 html-minifier 来缩小 html 文件。为此,我创建了这个应该能够执行此操作的小 node.js 文件

'use strict'

var fs = require('fs');
var minifier = require('html-minifier').minify;

var htmlFile = fs.readFileSync("users/email/test.html");
var output = minifier(htmlFile, {
    removeAttributeQuotes: true
});

process.stdout.write(output);

但是当我运行该程序时,出现以下错误。

TypeError: value.replace is not a function

知道为什么会这样。我使用的是 4.0.0 版的 html-minifier

最佳答案

由于您没有指定文本编码,readFileSync 返回了一个Buffer,而不是一个字符串。查看readFileSync documentation .

如果您知道要使用的编码,则可以将其指定为第二个参数:

var htmlFile = fs.readFileSync("users/email/test.html", "utf8");

关于javascript - html-minifier node.js TypeError : value. 替换不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56579180/

相关文章:

javascript - 从连接中排除文件(MINIT 插件)

javascript - 如何在 Gitlab CI 中缩小 Javascript 代码?

javascript - QML 中的基本 Javascript/数学……为什么不起作用?

javascript - TypeORM 自定义装饰器

javascript - 渲染 EJS 模板报错 this.templateText.replace is not a function

node.js - 以 CSV 格式获取 Google Drive 电子表格的内容

javascript - 使用 Java/Scala 压缩 CSS/Javascript

javascript - 使用 JQuery 构建树

javascript - Mongodb-用户验证失败

javascript - Babylon.js 去除球体的光反射