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'
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.
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.
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')
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,
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With