2

I'm using the fast-csv node library to parse a csv-file named myFile.csv. The code looks like this:

var csv = require("fast-csv");
var fs = require("fs");
var stream = fs.createReadStream('myFile.csv');
                csv
                    .fromStream(stream, {headers : true})
                    .on("data", function(data){
                         console.log("Start of parsing...");
                         console.log(data);
                    })
                    .on("end", function(data){
                         console.log(data);
                         console.log("End of parsing");
                    })

When I run it, I see the following two lines in node console:

1
End of parsing

It doesn't print "Start of parsing..." or any data from the csv file. What could be the reason for that? Thanks in advance!

3 Answers 3

0

Put the csv into a variable and then use stream.pipe() with it. Like this:

var csvStream = csv
                .fromStream(stream, {headers : true})
                .on("data", function(data) {
                    console.log("Start of parsing...");
                    console.log(data);
                })
                .on("end", function(data){
                    console.log(data);
                    console.log("End of parsing");
                });

stream.pipe(csvStream);
Sign up to request clarification or add additional context in comments.

1 Comment

Your suggestion is another way of writing the same code. I had some other problem with my code. I was running the 0.2.0 version of the fast-csv module. I updated it to the latest version 2.1.0 and now my old code as well as the one you suggested work perfectly fine. Thanks!
0

Just reinstall fast-csv module by using the following command:

npm install fast-csv --save

1 Comment

The solution is not reinstall fast-csv, the solution is to use a version which accepts that syntax.
0

Verify the types array in your tsconfig.json: Confirm that the types array in your tsconfig.json file includes the csv-parser entry. Double-check the spelling and casing to ensure it matches the package name exactly. Here's an example of how the updated types array should look: TypeScript:

"types": [
  "node",
  "webpack-env",
  "fast-csv"
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.