Jadeye
Jadeye

Reputation: 4329

TypeError: parse is not a function on csv-parse

I ran into this issue after setting up a simple app using:

Code is:

const parse = require('csv-parse');
const fs = require('fs');

const results = [];

fs.createReadStream('kepler_data.csv')
    .pipe(parse({
        comment: "#",
        columns: true,
    }))
    .on('data', (data) => {
        results.push(data);
    })
    .on('error', (err) => {
        console.log(err);
    })
    .on('end', () => {
        console.log(results);
        console.log('Done!');
    });

Running it resulted in:

index.js:7
    .pipe(parse({
          ^

TypeError: parse is not a function
    at Object.<anonymous> (/home/jadeye/node.js_workspace/PLANETS-PROJECT/index.js:7:11)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

Searching SO gave similar results but no actual solution.

Upvotes: 13

Views: 17596

Answers (10)

Yas_mine
Yas_mine

Reputation: 1

Use:

const {parse} = require('csv-parse'); 

instead of :

const parse = require('csv-parse');

Upvotes: 0

NOIBI ABDULSALAAM
NOIBI ABDULSALAAM

Reputation: 42

Destructuring the parse function worked for me.

const {parse} = require("csv-parse");

Upvotes: 1

dedan njoroge
dedan njoroge

Reputation: 21

Try destructuring the actual functions from the import.
Instead of const parse = require('csv-parse');
Use: const { parse } = require('csv-parse');

Upvotes: 2

user22144631
user22144631

Reputation: 21

const { parse } = require("csv-parse");

This works for me instead of this:

const parse = require('csv-parse');

Upvotes: 2

We Santos
We Santos

Reputation: 21

const fs = require("fs");
const {parse} = require("csv-parse");

const results = [];

fs.createReadStream("./kepler_data.csv").pipe(parse({
   comment: '#',
   columns: true,
})).on('data', (data) => {
   results.push(data)
}).on('end', () => {
   console.log(results);
   console.log('done 🔥');
})

This works for me

Upvotes: 2

Ismail
Ismail

Reputation: 111

  1. Try const {parse} = require('parse') instead of const parse = require('parse') OR
  2. Try csv-parse version instead "csv-parse": "^3.0.0" of "csv-parse": "^5.3.0"

csv-parse

Upvotes: 5

Hulisani Mudimeli
Hulisani Mudimeli

Reputation: 520

What worked for me was changing from "csv-parse" to "csv-parser"

Use this in package.json

"csv-parser": "^3.0.0"

Instead of:

"csv-parse": "v5.0.4"

Upvotes: 1

frank takou
frank takou

Reputation: 31

get the parse function it self in the csv-parse module by doing const {parse} = require("csv-parse");

and call .pipe(parse())

Upvotes: 0

yash sanghavi
yash sanghavi

Reputation: 430

Instead of using

const { parse } = require('csv-parse');

Try Following

const { parse } = require("csv-parse");

Upvotes: 1

Jadeye
Jadeye

Reputation: 4329

Solution was simple. If parse or any other function is not recognized with node require, try getting the function itself as:

const { parse } = require('csv-parse');

Something worth noting it the color scheme that emphasizes the difference between the const parse color = white, and the usage of the function parse inside pipe = green.

errornous call

And the colors without error where const { parse } is green, matchin the const call.

enter image description here

Upvotes: 33

Related Questions