Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for requring csv-parse if it is a part of the csv package

Tags:

node.js

this is probably just a simple gotcha, but I cannot figure out the syntax (new to node.js)

I have done a

npm install csv 

in my node.js project. The project's homepage can be found here

The following line runs without issue:

var csv = require('csv');

But when I need to use csv-parse functions (which is a part of the csv package) I cannot. Trying to require it yields a not found:

var parse = require('csv-parse');
Error: Cannot find module 'csv-parse'

I've tried a few variations:

var parse = require('csv()csv-parse');
var parse = require('csv.csv-parse');
var parse = require('csv().csv-parse');

thinking they had to reference the csv bit required just above it, but none seem to work. I could always just re-install just the csv-parse bit, but the website indicates that I shouldn't need to (as either are enough):

Run npm install csv to install the full CSV package or run npm install csv-parse if you are only interested by the CSV parser.

But sadly I cannot find any examples on the project page that work with just installing 'csv'

like image 988
Timothy Harding Avatar asked Jul 16 '15 19:07

Timothy Harding


People also ask

How to parse a CSV file using JavaScript?

Since a CSV file is essentially a text file with comma-separated values, we can use the FileReader class to read CSV files as a string and format it accordingly. In this tutorial, we learn how to parse CSV using JavaScript. To parse the CSV file directly, we can use the jquery-csv plugin.

Should I install CSV or CSV-parse or both?

If you still need the csv package as well, install both. This is necessary if you want to use the Synchronous API via require ('csv-parse/lib/sync') Show activity on this post. A problem may likely exist with the version of npm you are using.

Should CSV-parse be installed as a dependency or a module?

Instead of installing the csv package as a dependency directly, install csv-parse. If you still need the csv package as well, install both. This is necessary if you want to use the Synchronous API via require ('csv-parse/lib/sync')

What is CSV file in Java?

A CSV file is a comma-separated text file with double quotes as escape characters for a comma in a string element. In this tutorial, we will see how we can parse a CSV file using Java code example,


2 Answers

Its exported with csv, here's an example

var parse = require('csv').parse

As a note about requiring another modules dependencies: I have never found the need to do that, as most module authors, either export (as in this case) or provide a suitable abstraction. That said you could usually require a dependency of an installed module with the following form:

var dep = require('{module}/node_modules/{dependency}');

In this case:

var parse = require('csv/node_modules/csv-parse');
// require('csv').parse === require('csv/node_modules/csv-parse') -> true

But as I said, I have never had to do this.

like image 117
undefined Avatar answered Sep 30 '22 13:09

undefined


I ran into a similar problem with Node 8 (worked fine with Node 7). This worked for me:

Instead of installing the csv package as a dependency directly, install csv-parse. If you still need the csv package as well, install both.

"dependencies": {
  "csv": "^2",
  "csv-parse": "^2"
}

This is necessary if you want to use the Synchronous API via require('csv-parse/lib/sync')

like image 31
Marco Roy Avatar answered Sep 30 '22 13:09

Marco Roy