Search code examples
javascripthtmlnode.jsjs-beautify

js-beautify for html has no method 'beautify'


I am trying to use js-beautify for html in a node.js application:

var htmlBeautifier = require('js-beautify').html;
...
res = htmlBeautifier.beautify(html);
...

But I get:

...
res = htmlBeautifier.beautify(html,{});
                         ^
TypeError: Object function (html_source, options) {
    return style_html(html_source, options, js_beautify.js_beautify, css_beautify.css_beautify);
} has no method 'beautify'

The documentation about using js-beautify for html does not provide much information. How is one supposed to use js-beautify for html?


Solution

  • According to the documentation (which could do with a bit more detail), html is the function, not an object with the function as a property. So:

    var htmlBeautifier = require('js-beautify').html;
    //...
    res = htmlBeautifier(html);
    

    or

    var htmlBeautifier = require('js-beautify');
    //...
    res = htmlBeautifier.html(html);