const htmlparser = require("htmlparser2");
const parser = new htmlparser.Parser({
onopentag: (name, attrib) => {
if (name=='p') console.log('a paragraph element is opening');
}
}, {decodeEntities: true});
parser.write(`<!DOCTYPE html><p>Hello world</p>`);
parser.end();
// console output: 'a paragraph element is opening'