Zend certified PHP/Magento developer

How to use critical-css-generator in Magento2?

Does anyone know how one is suppose to correctly implement critical-css-generator to auto-generate ciritcal.css files in Magento2?
I tried to replicate the instructions here:
https://www.rohanhapani.com/how-to-enable-and-generate-css-critical-path-in-magento-2/

I added this block of code

const critical = require('critical-css-generator');
critical.generate({
    url: 'https://<my website>',
    path: '/var/www/html/m240/app/design/frontend/<my-theme>/web/css/test_critical.css',
    viewport: true
});

insdie module.exports in the Gruntfile.js.
But when I run grunt exec this error pops up in the console:

no exec targets found.

and sometmes this error:

Unable to resolve source file for frontend/<my-theme>/en_us/css/styles-l.css.less

Where is it getting this styles-l.css.less?

Can anyone help? I can not find any tutorials online.

What do you think is the best way to generate critical css files in Magento 2?