-1

I'm trying to read a csv file using node.js. I'm getting the error "unexpected end of input". The following is the code I've used:

const csv = require('csv-parser');
const fs = require('fs');
const fast = require('fast-csv');


fs.createReadStream('filename.csv')
.pipe(csv())
.on('data', (row) => {
    console.log(row);
})
.on('end', (row) => {
    console.log('CSV file successfully processed');

I've taken the code from here.

when I run the above code in command mode using "node sample.js", the following is the error:

C:\Users\learningsql\Desktop\filter>node sample.js
C:\Users\learningsql\Desktop\filter\sample.js:12
    console.log('CSV file successfully processed');


SyntaxError: Unexpected end of input
[90m    at wrapSafe (internal/modules/cjs/loader.js:1047:16)[39m
[90m    at Module._compile (internal/modules/cjs/loader.js:1097:27)[39m
[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)[39m
[90m    at Module.load (internal/modules/cjs/loader.js:977:32)[39m
[90m    at Function.Module._load (internal/modules/cjs/loader.js:877:14)[39m
[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)[39m
[90m    at internal/main/run_main_module.js:18:47[39m

Any help is appreciated. Thanks.

2
  • I got it. If anyone's curious, I used this code const parse = require('csv-parse'); const fs = require('fs'); const csvData= []; fs.createReadStream(__dirname + '/filename.csv') .pipe( parse({ delimiter: ',' }) ) .on('data', function (dataRow) { csvData.push(dataRow); }) .on('end', function () { console.log(csvData); }); Commented May 12, 2020 at 9:50
  • 1
    This code is missing the closing brackets from the end });. Commented May 12, 2020 at 9:52

1 Answer 1

1

Try this:

const csv = require('csv-parser');
const fs = require('fs');
const fast = require('fast-csv');


    fs.createReadStream('accounts.csv')
        .pipe(csv())
        .on('data', (row) => {
            console.log(row);
        })
        .on('end', () => {
            console.log('CSV file successfully processed')
        });

You were missing the closing parentheses and bracket

Sign up to request clarification or add additional context in comments.

11 Comments

thanks. I'm able to read the csv file and get all the columns into arrays, but I need only one specific column's elements as an array. Any suggestions?
What does "addresses" mean here? Do I replace address_1 with column name of the column I want the records of? Please refer to this link to understand what I'm trying to do. Thanks for taking time to help me out by the way.
Correct. Here is the new link where I have changed the data set to reflect your particular data set. Hope this helps.
Hey, thanks a lot. It did work, just had to change the column name in my csv. If there's a space in the column name, apparently it doesn't work. So I changed the Country Name header to CountryName and it worked!
Great to hear! Could you accept the answer in that case ?
|

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.