First of all, if you set your encoding correctly (on the html page and in the database or convert before inserting/after selecting) you shouldn't need to convert special chars to HTML entities to avoid the described behaviour. htmlspecialchars() should be used to prevent people from posting HTML (converting < and > to < and >).
If you still need to escape HTML special characters, take a look at this NPM package : https://www.npmjs.com/package/html-entities
Use it like this :
const Entities = require('html-entities').AllHtmlEntities;
const entities = new Entities();
console.log(entities.encode("éèàâê");
will output
éèàâê