A local css file that I read using node's fs.readFile contains UTF-8 BOM headers. How do I get rid of it?

I used node"s fs.readFile to read the contents of a local css file cssData,
and then created a style tag in the head tag of the page, assigning cssData to style.innerHtml,
but the style did not take effect. I checked and found that the style in the front of the style tag was more than one: &-sharp65279, checked the bom head of utf-8 when I went online and said, I output cssData did not find this, do not know how to remove this. How can I remove this BOM header from the style tag?

Mar.21,2021

generally, those containing BOM headers are generated under windows (added with tools such as notepad)
if you can control this file, you can resave it without BOM headers.
otherwise, you can use the following additional function to remove

function readText(pathname) { //utf-8 BOM
    var bin = fs.readFileSync(pathname);

    if (bin[0] === 0xEF && bin[1] === 0xBB && bin[2] === 0xBF) {
        bin = bin.slice(3);
    }

    return bin.toString('utf-8');
}

1. First of all, is your css file under your own control? If it is controllable, can you save it according to the uft-8 no BOM standard and then read it to ok?
2. If 1 can't solve it, can you set style.innerHtml= str.replace ('answer',')


I just tried your method to solve my problem, but I switched to asynchronous: fs.readFile () is not good, this is why. What should I do if it is asynchronous?

Menu