Last active
July 26, 2024 07:16
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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