Save CSV file in UTF-8 with BOM

Fix Korean text from being corrupted using Node.js or VS Code

Hyunbin
2 min readOct 5, 2022

Recent versions of Microsoft Excel can open csv files directly. No need to use the Get External Data From Text feature. However, csv file including Korean text often breaks, despite being saved in a UTF-8 encoding.

Korean text is corrupted in a Excel spreadsheet (source)

The culprit is the encoding. Using the UTF-8 with BOM encoding fixes the problem. This can be achieved by adding leading (0xEF, 0xBB, 0xBF) or \ufeff in-front of a UTF-8 string. Following is a Node.js code example.

import { writeFile } from 'node:fs/promises';// Default encoding of fsPromises.writeFile() is UTF-8await writeFile(
'sample.csv',
'\ufeff' + '단어,뜻',
);

When editing a csv file using VS Code, there is a Change File Encoding item in the command palette. Use the ‘Save with Encoding’ to change the csv file’s encoding from UTF-8(default) to UTF-8 with BOM(utf8bom).

Changing a text file’s encoding using VS Code’s command palette

It is also possible to set a VS Code workspace to always use UTF-8 with BOM in all csv files. Reference the following .vscode/settings.json file (code).

{
"[csv]": {
"files.encoding": "utf8bom"
}
}

Note that this setting only applies to new files created in VS Code. Existing files’ encoding should be converted manually. It is also possible to copy the entire content and paste it to a newly saved CSV file. It will be saved in UTF-8 with BOM.

--

--

Hyunbin

Node.js and web developer using TypeScript. Undergraduate in Seoul National University.