Format CSV output with fast-csv

A practical review of fast-csv's formatting options: what they change in the written file, how quoting, delimiters and headers behave, and where to verify before sharing CSV output.

fast-csv is a Node.js library for parsing and writing delimited text. Its formatting side, published separately as @fast-csv/format and also re-exported by the umbrella fast-csv package, turns JavaScript rows into CSV or TSV text. This review covers what the formatter changes in your data, which options matter most, and where its output can still cause problems once someone opens, copies or imports it. No test results or benchmarks were supplied for this article. The behavior described here comes from the library's documented API, and anything that depends on a specific spreadsheet or viewer is flagged as something to verify yourself.

Is fast-csv formatting suitable for your use?

fast-csv is a good fit if you already work in Node.js and need to stream rows to a file, an HTTP response or another stream without building the whole document in memory. Its main benefit is that it handles CSV escaping correctly by default while still letting you change the delimiter, quoting, header handling and line endings. The decisive tradeoff is that it only produces text. It does not know what application will open that text, so compatibility details such as line endings, byte order marks and spreadsheet formula handling are your responsibility.

A different approach may serve you better in some cases. If people will read the result directly in a message or document, a simple aligned table or prose summary is often easier to read than raw CSV. If the data is nested rather than tabular, a format such as JSON keeps the structure that CSV would flatten. If you are not in a JavaScript environment, a library native to your stack will be simpler than adding Node.js.

What the formatter actually changes

Formatting with fast-csv changes the stored characters of the output. It is not a visual effect. It does not reverse text, mirror glyphs, substitute Unicode look-alike characters or apply any display styling. It takes each field value, converts it to a string, adds quotes and escapes where needed, and joins fields and rows with the delimiters you configure. Characters inside your values, including accented letters, combining marks and multi-code-point emoji, pass through as JavaScript strings without being reordered or split. Whether they display correctly afterward depends on the encoding and the application that reads the file.

The formatter changes these parts of the output:

  • Delimiter: the character between fields, a comma by default. Set it to a tab for TSV.
  • Row delimiter: the line ending between rows, a line feed by default.
  • Quoting: fields that contain the delimiter, the quote character or a line break are wrapped in quotes. The quoteColumns and quoteHeaders options can force quoting for all columns or for selected ones.
  • Escaping: quote characters inside a quoted field are escaped. By default this is done by doubling them.
  • Headers: a header row can be taken from the keys of the first object (headers: true) or supplied as an array, which also sets column order and selects which columns appear.
  • Ending and encoding markers: includeEndRowDelimiter adds a final line ending, and writeBOM prepends a UTF-8 byte order mark. Both are off by default.
  • Row content: an optional transform function can rewrite each row before it is written.

A minimal example and its output

The stream interface writes objects and derives the header row from their keys:

const { format } = require('@fast-csv/format');

const stream = format({ headers: true });
stream.pipe(process.stdout);

stream.write({ name: 'Ada', note: 'Says "hi", often' });
stream.end();

With the default settings this should produce the following output:

name,note
Ada,"Says ""hi"", often"

The note field contains a comma and quote characters. It is therefore wrapped in quotes, and each inner quote is doubled. The name field contains neither, so it is left unquoted. Because includeEndRowDelimiter defaults to false, the output has no trailing line ending. This matters if another tool appends rows to the file or expects every line to end with a terminator.

If you want a string instead of a stream, for example to produce TSV from arrays, writeToString returns a promise:

const { writeToString } = require('@fast-csv/format');

writeToString(
  [['id', 'label'], ['1', 'first'], ['2', 'second']],
  { delimiter: '\t' }
).then((tsv) => console.log(tsv));

Similar helpers write to a buffer, a file path or an existing stream. Choose based on where the output goes. Use the stream form when the data is large or arrives incrementally.

Shaping rows before they are written

The transform option is the right place for output-specific changes such as renaming fields, formatting dates or trimming values. It keeps those changes out of your source data:

const stream = format({
  headers: true,
  transform: (row) => ({
    name: row.name.trim(),
    joined: row.createdAt.toISOString().slice(0, 10),
  }),
});

Header handling interacts with transforms. When headers is true, the header row comes from the first row the formatter sees, which here is the transformed row. Supplying an explicit headers array makes column order predictable. It also keeps unexpected keys from silently changing the header.

Will the output stay usable after sharing or importing?

Correct quoting means a compliant CSV parser should read the fields back exactly as written, including embedded commas, quotes and line breaks. Problems usually come from the receiving application, not from the file structure. The points below are documented CSV conventions or common compatibility concerns. None of them were tested for this article against a named spreadsheet, database importer or assistive technology, so check them against your actual destination.

  • Line endings: the CSV convention in RFC 4180 uses carriage return plus line feed, while fast-csv defaults to a line feed alone. If your consumer is strict, set rowDelimiter to '\r\n'.
  • Encoding detection: writeBOM is commonly used so that spreadsheet applications recognize the file as UTF-8 and display non-ASCII text correctly. Whether it is needed or helpful depends on the application and version, and some programmatic parsers treat the BOM as part of the first header name.
  • Delimiter and locale: some spreadsheet locales expect a semicolon rather than a comma. If the file opens with every value in one column, check the delimiter before assuming the data is wrong.
  • Formula injection: values beginning with characters such as an equals sign can be interpreted as formulas when the file is opened in a spreadsheet. The formatting options covered here do not neutralize this. If untrusted input reaches a spreadsheet user, handle it in a transform and check the current documentation for any dedicated option.
  • Copy and paste: pasting raw CSV into chat or email keeps the characters, but quoted line breaks and long rows quickly become hard to read. Share the file itself, or paste a short plain-text table instead.

Accessibility depends on where the data ends up. A CSV file has no reading order beyond its rows and columns. How a screen reader announces it depends on the application that opens it, and no scoped test of that was available here. If the data is meant to be read rather than processed, an accessible table in a document or web page with proper header cells is usually a better deliverable than the CSV itself. Searching and editing are generally unaffected because the content stays ordinary text. Heavily quoted fields with doubled quotes are harder to edit by hand, which is another reason to make changes in code instead of in a text editor.

Gaps in this review

This review does not include performance measurements, version-specific changes, or compatibility tests with particular spreadsheets, databases or screen readers, because none were supplied. Option names and defaults should be confirmed against the documentation for the version you install. If CSV output matters to a downstream process, write a sample file containing commas, quotes, line breaks, non-ASCII text and an empty value, then open it in the exact tool that will consume it before you rely on it.