A JavaScript HTTP Trigger Azure Function posts activity in an online customer community in Slack.
It has worked successfully for years but upon upgrade from version ~2 to ~4 has begun throwing an Entities is not a constructor
error on the following code:
var Entities = require('html-entities').AllHtmlEntities;
var entities = new Entities();
var title = entities.decode(subject)
The goal is to use html-entities on the content of the subject
variable.
The above code is shown in context in this short GitHub script.
Most solutions to this error seem to recommend using import
instead of requires
and changing the code accordingly. However, the Azure Function complains when an import statement is in the body of the module, at the top of the module, and outside the module, even though by all accounts the top of the module is the correct place for it.
Many other solutions recommend putting various iterations of the code inside and outside new functions in various configurations, but I haven't been successful. I can share the examples of my attempts if helpful.
Thank you for any patience as I am not a programmer - just automating solutions as part of my job. I am grateful for any recommendations, including on whether this is not an ideal way to achieve my goal.
See the CHANGELOG.md
From the CHANGELOG.md
:
htmlEntitiesInstance.encode(text)
-> encode(text)
Before:
import {AllHtmlEntities} from 'html-entities';
const entities = new AllHtmlEntities();
console.log(
entities.encode('<Hello & World>')
);
After:
import {encode} from 'html-entities';
console.log(
encode('<Hello & World>')
);
Your code should be:
var decode = require('html-entities').decode;
var title = decode(subject)