Transform CSV rows with fast-csv
A practical review of row transformation in fast-csv: what the transform step changes, how to keep data meaning intact, and when CSV-to-TSV output stays usable after sharing.
fast-csv is a Node.js library for parsing and formatting delimited text as streams. Its row transformation step lets you reshape each record as it moves from input to output: rename fields, trim values, convert types, drop columns, or switch the delimiter from commas to tabs. This review covers what that step actually changes, where it can quietly alter the meaning of your data, and when the output stays readable once someone opens or pastes it elsewhere.
A note on scope: no benchmark results, version-specific measurements or independent test data were available for this review. Statements about behavior below describe the library's documented API and general Node.js stream principles, not timed or measured results. Despite the package name, no claim is made here about how fast it is relative to other CSV tools.
Is a fast-csv row transform suitable for your use?
It is a good fit when you need to process CSV or TSV files record by record in Node.js, especially files too large to load comfortably into memory at once. The main benefit is that parsing, transformation and formatting can run as one streaming pipeline, so each row is handled and written out without holding the whole file.
The decisive tradeoff is that you write code for every rule. There is no visual preview, no automatic type detection and no built-in schema. Every value arrives as a string, and any conversion you apply is your responsibility to get right, including edge cases like empty cells, leading zeros and locale-specific number formats.
Plain, untransformed text is preferable when the file only needs to be read or archived, when a downstream system expects the exact original values, or when a one-off edit is easier to do and check in a spreadsheet. If nobody will rerun the job, a script may add more risk than it removes.
What the transformation actually changes
In this context, "transform" means changing the data in each row, not changing how text looks. Nothing is reversed, mirrored or restyled. The transform function receives a row and returns a new row, and only the returned value continues down the pipeline. The source file on disk is never modified; changes exist only in the output you write.
There are two places a transform can happen. On the parse side, the parser stream exposes a transform method that runs on each parsed row. On the format side, the formatter accepts a transform as well, applied just before a row is written. Parse-side transforms are the usual place for cleaning and reshaping; format-side transforms suit last-minute output adjustments.
With headers enabled, each row is an object keyed by column name. Without headers, each row is an array of strings. This matters because a transform written for one shape will fail or silently produce wrong output with the other.
const fs = require('fs');
const csv = require('fast-csv');
fs.createReadStream('input.csv')
.pipe(csv.parse({ headers: true }))
.transform(row => ({
id: row.id,
name: row.name.trim(),
total: row.total === '' ? '' : Number(row.total)
}))
.on('error', err => console.error(err))
.pipe(csv.format({ headers: true, delimiter: '\t' }))
.pipe(fs.createWriteStream('output.tsv'));
This example reads a comma-delimited file, trims names, converts totals to numbers and writes tab-delimited output. Only the three listed fields survive; any other column in the input is dropped because the transform does not return it. The check for an empty total is deliberate: converting an empty string with Number produces 0, which would turn "no value recorded" into "zero", a change in meaning rather than format.
Synchronous versus asynchronous transforms
A transform can return the new row directly, or it can accept a callback as a second argument for work that has to wait, such as a lookup. In the callback form, pass an error as the first argument to stop processing, or null followed by the transformed row.
.transform((row, cb) => {
lookupRegion(row.zip)
.then(region => cb(null, { ...row, region }))
.catch(cb);
})
Validation alongside transformation
The parser also offers a validate method. Rows that fail validation are not passed on as normal data; they are emitted through a separate data-invalid event. That gives you a record of what was excluded instead of silently losing rows, which is often the difference between a trustworthy conversion and one that merely looks complete.
.validate(row => row.id !== '')
.on('data-invalid', (row, rowNumber) => {
console.warn(`Skipped row ${rowNumber}: missing id`);
})
Where meaning can drift
- Type conversion: values parsed as strings keep leading zeros in postal codes or account numbers; converting them to numbers removes those zeros permanently in the output.
- Empty versus zero: empty cells and literal zeros are different facts; decide explicitly how each should be written.
- Trimming: the trim, ltrim and rtrim parser options remove whitespace that is sometimes significant, such as fixed-width codes.
- Dropped columns: a transform that returns a new object keeps only the fields you list, so new columns added to the source later will disappear without warning.
- Header handling: renaming or discarding headers changes how downstream tools map columns, even if every value is intact.
- Row counts: validation, maxRows, skipRows and ignoreEmpty can all reduce the number of output rows; compare input and output counts before trusting the result.
Will the output stay usable after copying or sharing?
Delimited output is plain text, so it carries no styling that could break and nothing that assistive technology must interpret beyond ordinary characters. Usability problems come instead from how the receiving program interprets delimiters, quotes and encoding.
| Concern | What to check | Relevant formatter option |
| Commas or tabs inside values | Values containing the delimiter must be quoted or they split into extra columns | quote, quoteColumns |
| Quotes inside values | Embedded quote characters need escaping | escape |
| Line breaks inside values | Quoted multiline cells may not survive pasting into every tool | quote |
| Non-ASCII characters | Some spreadsheet programs guess the encoding unless a byte order mark is present | writeBOM |
| Header row | Confirm whether the recipient expects headers | headers, writeHeaders |
| Trailing newline | Some tools expect a final line ending | includeEndRowDelimiter |
Tab-delimited output is often easier to paste directly into spreadsheet cells than comma-delimited text, because tabs rarely appear inside ordinary values. That is a general tendency, not a guarantee; behavior depends on the specific application and its settings, and no destination-specific tests were available for this review. If the output will be opened in a particular program, test a sample file containing commas, quotes, line breaks and accented characters before relying on it.
Keep a plain copy of the original file whenever the transformed version will be shared. If a recipient needs to verify a value, audit a calculation or re-import into a system that expects the original layout, the untransformed source is the reference point.
Handling errors in the pipeline
A Node.js stream's pipe method does not forward errors from one stream to the next. An error in the read stream, parser or formatter must be handled on that stream, or the pipeline can stall or leave a partially written file. Using the stream pipeline utility from Node's standard library, which reports an error from any stage through a single callback or promise, is a more reliable way to tell whether the whole conversion finished.
const { pipeline } = require('stream/promises');
await pipeline(
fs.createReadStream('input.csv'),
csv.parse({ headers: true }).transform(cleanRow),
csv.format({ headers: true, delimiter: '\t' }),
fs.createWriteStream('output.tsv')
);
Gaps in this review
- No performance measurements were available, so no claims are made about throughput or memory use on large files.
- Option names and behavior should be confirmed against the documentation for the version you install, since APIs can change between major releases.
- No testing of specific spreadsheet programs or import tools was available; compatibility statements are general and should be verified with your own sample files.
- No information was available about the library's handling of unusual encodings beyond what its options expose.
Bottom line
fast-csv's row transform is well suited to repeatable, code-driven cleanup and CSV-to-TSV conversion in Node.js, particularly when files are large or the job will run more than once. Its reliability depends on decisions you make explicitly: how empty cells, numeric strings and dropped columns are handled, and whether invalid rows are logged rather than lost. For one-off edits, or when the exact original values must be preserved, leave the data as plain text and work on a copy.