Solution 1 :

Maybe it’s an asynchronous function, and you need to do

var result = await minify(pug_html, {
    removeAttributeQuotes: true,
    minifyJS:true,
    minifyCSS:true,
    removeComments:true,
    collapseWhitespace:true,
    collapseInlineTagWhitespace:true
  });

with the await keyword, which makes it “wait” for the response from the function

Solution 2 :

See this link to learn more about promises. Essentially, you must wait for minifyJS to return the HTML by waiting for it (using .then((arg)=>{}). Also see the bottom of this readme

  minify(pug_html, {
    removeAttributeQuotes: true,
    minifyJS:true,
    minifyCSS:true,
    removeComments:true,
    collapseWhitespace:true,
    collapseInlineTagWhitespace:true
  }).then((res) => {result = res});

Problem :

I don’t want to install the node modules again and again so I found a post here.

It uses npm link to let me require global packages.

However, when I use the method, the html-minifier-terser is not working properly. The minify function returns Promise { <pending> } instead of the HTML.

But it works fine with the same build.js while in that project, the pug and html-minifier-terser are install locally by npm install not npm install -g

Here is the code:

const fs = require('fs');
// Compile the source code
const pug = require('pug');
// Compile the source code


buildFile('index');

function buildFile(filename) {

  var fs_content = fs.readFileSync(`./data/page-${filename}.json`);
  var options = JSON.parse(fs_content);

  const compiledFunction = pug.compileFile(`./pugs/${filename}.pug`,{});

  // Render a set of data
  var pug_html = (compiledFunction(options)); // this works fine.
  var minify = require('html-minifier-terser').minify;
  var result = minify(pug_html, {
    removeAttributeQuotes: true,
    minifyJS:true,
    minifyCSS:true,
    removeComments:true,
    collapseWhitespace:true,
    collapseInlineTagWhitespace:true
  });
  console.log(result); // Returns Promise { <pending> } instead of the HTML
}

Update:

I tried await but it fails too:

  var result = await minify(pug_html, {
               ^^^^^

SyntaxError: await is only valid in async function
    at new Script (vm.js:79:7)
    at createScript (vm.js:251:10)
    at Object.runInThisContext (vm.js:303:10)

Comments

Comment posted by github.com/terser/html-minifier-terser

eh, it’s always supposed to return a promise isn’t it?

Comment posted by AGamePlayer

weird, in another project with the same

Comment posted by github.com/terser/html-minifier-terser/blame/…

You are correct, that changed “recently”, see this readme from before the change 2 months ago:

Comment posted by AGamePlayer

Thanks but it doesn’t work. Please see my updates in the original post.

Comment posted by Kevin B

it fails because await can only be used in an async function, so you’d’ have to make the function that code is in async by prepending the function declaration line with

Comment posted by Eisverygoodletter

Sorry, the function name needs to be changed to

Comment posted by Eisverygoodletter

Or you can just use

Comment posted by Community

Please add further details to expand on your answer, such as working code or documentation citations.

By