Skip to content

Instantly share code, notes, and snippets.

@lgk-bsw
Last active July 26, 2024 07:16
Show Gist options
  • Save lgk-bsw/39bfab9fa6a5f7bc25d938631f534d40 to your computer and use it in GitHub Desktop.
Save lgk-bsw/39bfab9fa6a5f7bc25d938631f534d40 to your computer and use it in GitHub Desktop.
Read files with "UTF-8" and "UTF-8 with BOM" using Node.js fs
const fs = require("fs")
const path = require("path")
const filePath = path.resolve(__dirname, "index.md")
/**
* By default, Visual Studio saves files with a BOM (Byte Order Mark) which can be a problem when reading files in Node.js.
* The replace function below removes the BOM from the file.
*/
function readFileSync(filePath) {
return fs.readFileSync(filePath, "utf8").replace(/^\uFEFF/, "")
}
const content = readFileSync(indexMdPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment