It treats each element in the array as an array, so '456' is treated as an array with the elements 4,5,6.

A strange workaround I did was to loop through the array and put each element inside its own array, so it would be:

var newArray = [];

 for( var i = 0; i < tempArray.length; i++){
 var arr = [];
 arr.push(tempArray[i]);
newArray.push(arr);
}

And then write the "newArray".

But otherwise if you write row by row, you can put each element inside a bracket, like:

var fast_csv = fastcsv.createWriteStream();
var writeStream = fs.createWriteStream("outputfile.csv");
fast_csv.pipe(writeStream);

for(var i = 0; i < tempArray.length; i++){
    fast_csv.write( [ tempArray[i]  ] );             //each element inside bracket
    }
fast_csv.end();
Answer from Galivan on Stack Overflow
🌐
npm
npmjs.com › package › fast-csv
fast-csv - npm
May 6, 2026 - CSV parser and writer. Latest version: 5.0.7, last published: 2 months ago. Start using fast-csv in your project by running `npm i fast-csv`. There are 958 other projects in the npm registry using fast-csv.
      » npm install fast-csv
    
Published   May 06, 2026
Version   5.0.7
🌐
C2fo
c2fo.github.io › fast-csv
Fast-CSV | Fast-CSV
CSV Parser and Formatter · Get Started · Built on top of the Node.js Stream API, so you can work with APIs you are familiar with. Out of the box formatting and parsing of any delimiter separated file.
Discussions

Read and Write CSV using fast-csv package in Node.js - Stack Overflow
I am trying to write a simple node program that reads a csv file, extracts a column (say second) and writes it to another CSV file. I am reading the contents to an array and then writing that array... More on stackoverflow.com
🌐 stackoverflow.com
FastCSV: 5X faster than encoding/csv
So You Want To Write Your Own CSV code? https://tburette.github.io/blog/2014/05/25/so-you-want-to-write-your-own-CSV-code/ More on reddit.com
🌐 r/golang
9
22
September 4, 2016
Reading from file with fast-csv and writing into an array
I'm using fast-csv's fromPath() method to read data from a file. I would like to write this data into an array (which I will subsequently sort). I would expect the code below to work for this purpo... More on stackoverflow.com
🌐 stackoverflow.com
Show HN: uDSV.js – A faster CSV parser
I know CSV parsers (especially in JS) aren't terribly exciting and someone writes a "better" one every week · I'm in the middle of my parental leave, and this was a project that came out of me looking for the fastest/smallest CSV parser. It all started so innocently, and then turned into a ... More on news.ycombinator.com
🌐 news.ycombinator.com
23
67
September 16, 2023
🌐
GitHub
github.com › c2fo › fast-csv
GitHub - C2FO/fast-csv: CSV parser and formatter for node · GitHub
Fast-csv is library for parsing and formatting CSVs or any other delimited value file in node.
Starred by 1.8K users
Forked by 217 users
Languages   TypeScript 99.0% | JavaScript 1.0%
🌐
FastCSV
fastcsv.org
Welcome to FastCSV | FastCSV
FastCSV is a high-performance CSV parser and writer for Java licensed under the MIT open source license.
🌐
DEV Community
dev.to › chriscmuir › fast-csv-for-csv-files-21a1
fast-csv for CSV files - DEV Community
March 6, 2021 - In looking at the feature set, fast-csv is comprised of 'parse' and 'format' routines for ingesting and transforming CSV files. It also supports streams for fast processing of large files.
Find elsewhere
🌐
Reddit
reddit.com › r/golang › fastcsv: 5x faster than encoding/csv
r/golang on Reddit: FastCSV: 5X faster than encoding/csv
September 4, 2016 - I think incorporating a loose mode parser (as opposed to strict) for efficiency with the caveat you're restricted to certain delimiters would be useful to the standard library. Then again, writing a CSV parser isn't particularly difficult that you can just build your own if performance is needed. ... Yeah, splitting on commas is faster at the expense of correctness.
Top answer
1 of 4
14

Well, I know that this question has been asked a long back but just now I got to work with CSV file for creating API with node js. Being a typical programmer I googled "Reading from a file with fast-csv and writing into an array" well something like this but till date, there isn't any proper response for the question hence I decided to answer this.

Well on is async function and hence execution will be paused in main flow and will be resumed only after nonasync function gets executed.

var queryParameter = ()=> new Promise( resolve =>{
   let returnLit = []
   csv.fromPath("<fileName>", {headers : true})
      .on('data',(data)=>{
          returnLit.push(data[<header name>].trim())
      })
      .on('end',()=>{
          resolve(returnLit)
      })
})
var mainList = [];
queryParameter().then((res)=>mainList = res)

If you want to validate something pass argument into queryParameter() and uses the argument in validate method.

2 of 4
8

The "on data" callback is asynchronous, and the commands that follow the callback will run before the callback finishes. This is why the code does not work, and this reasoning has been pointed out by others who have posted answers and comments.

As for a good way to accomplish the task, I have found that using the "on end" callback is a good fit; since the intention here is to "do something" with the whole data, after the file has been read completely.

var dataArr = [];
csv.fromPath("datas.csv", {headers: true})
.on("data", data => {
  dataArr.push(data);
})
.on("end", () => {
  console.log(dataArr.length);
  // > 4187
});
🌐
GitHub
github.com › osiegmar › FastCSV
GitHub - osiegmar/FastCSV: Fast, lightweight, and RFC 4180 compliant CSV library for Java. Zero dependencies, ~90 KiB. Trusted by Apache NiFi, JUnit, and Neo4j. · GitHub
FastCSV: fast, lightweight, and easy to use — the production-proven CSV library for Java.
Starred by 684 users
Forked by 106 users
Languages   Java
🌐
SourceForge
sourceforge.net › projects › fast-csv.mirror
Fast CSV download | SourceForge.net
May 6, 2026 - Download Fast CSV for free. CSV parser and formatter for node. A high-performance Node.js library for parsing and formatting CSV data efficiently.
🌐
Hacker News
news.ycombinator.com › item
Show HN: uDSV.js – A faster CSV parser | Hacker News
September 16, 2023 - I know CSV parsers (especially in JS) aren't terribly exciting and someone writes a "better" one every week · I'm in the middle of my parental leave, and this was a project that came out of me looking for the fastest/smallest CSV parser. It all started so innocently, and then turned into a ...
🌐
npm
npmjs.com › package › @fast-csv › parse
@fast-csv/parse - npm
May 6, 2026 - fast-csv parsing package. Latest version: 5.0.7, last published: 9 days ago. Start using @fast-csv/parse in your project by running `npm i @fast-csv/parse`. There are 102 other projects in the npm registry using @fast-csv/parse.
      » npm install @fast-csv/parse
    
Published   May 06, 2026
Version   5.0.7
🌐
C2fo
c2fo.github.io › fast-csv › docs › parsing › methods
Methods | Fast-CSV
Creates a Csv Parsing Stream that can be piped or written to. This is the main entry point and is used by all the other parsing helpers. TypeScript · JavaScript · Copy · import * as fs from 'fs'; import { parse } from 'fast-csv'; fs.createReadStream('my.csv') .pipe(parse()) .on('error', ...
🌐
jsDelivr
jsdelivr.com › package › npm › fast-csv
fast-csv CDN by jsDelivr - A CDN for npm and GitHub
May 6, 2026 - A free, fast, and reliable CDN for fast-csv. CSV parser and writer
Published   Sep 12, 2012
🌐
FastCSV
fastcsv.com
FastCSV - Lightning-fast large CSV file opening!
Open, analyze, and query massive CSV files with SQL or natural language using our AI assistant. FastCSV is a free, offline tool that gives you lightning-fast access to your data without uploads. Secure, private, and powerful.
🌐
Pandas
pandas.pydata.org › docs › user_guide › 10min.html
10 minutes to pandas — pandas 3.0.4 documentation
In [138]: pd.read_csv("foo.csv") Out[138]: Unnamed: 0 0 1 2 3 4 0 0 4 3 1 1 2 1 1 1 0 2 3 2 2 2 1 4 2 1 2 3 3 0 4 0 2 2 4 4 4 2 2 3 4 5 5 4 0 4 3 1 6 6 2 1 2 0 3 7 7 4 0 4 4 4 8 8 4 4 1 0 1 9 9 0 4 3 0 3
🌐
npm
npmjs.com › package › @fast-csv › format
fast-csv/format
August 4, 2025 - fast-csv formatting module. Latest version: 5.0.7, last published: 2 months ago. Start using @fast-csv/format in your project by running `npm i @fast-csv/format`. There are 123 other projects in the npm registry using @fast-csv/format.
      » npm install @fast-csv/format
    
Published   May 06, 2026
Version   5.0.7
🌐
Leanylabs
leanylabs.com › blog › js-csv-parsers-benchmarks
JavaScript CSV Parsers Comparison
Although it aims for maximum speed, the performance is on-par with csv-parse and slower than PapaParse. ... Fast-CSV combines packages to format and parse CSV files. The name suggests it must be quite fast, but it’s the slowest among all parsers ...
🌐
FastCSV
fastcsv.org › architecture › architecture
Architecture | FastCSV
This document describes the architecture of FastCSV. It is intended for developers who want to understand the inner workings of the library. ... This section provides definitions for key terms related to the CSV format, as per the CSV specification RFC 4180.