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

this is probably just a simple combination, but i can't figure out the syntax (new to node.js)

I did

npm install csv 

      

in my node.js project. The project home page is here

The following line works without issue:

var csv = require('csv');

      

But when I need to use the csv-parse functions ( which is part of the csv package ) I cannot. Trying to demand it fails:

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

      

I've tried several options:

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

      

thinking they should have referenced the csv bit only needed above it, but none seem to work. I could always just reset the csv-parse bit only, but the website indicates that I don't need to (since that's enough):

Run npm install csv to install the complete CSV package, or run npm install csv-parse if you're only interested in the CSV parser.

But unfortunately I cannot find any examples on the project page that only work with the 'csv' installation

+3


source to share


3 answers


Exported from csv

, here's an example

var parse = require('csv').parse

      

As a side note on the requirements for other module dependencies: I never found it necessary to do this, like most module authors, either export (as in this case) or provide a suitable abstraction. However, you could usually require

install the dependent part 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 never had to do it.

+5


source


There may be a problem with the npm version you are using. We used an older version of npm (2.14.12), and after npm install csv, dependent modules including csv-parse were placed in csv / node_modules / csv-parse, not as equal as expected. When we upgraded to npm (3.8.0), this fixed the problem after uninstalling (npm uninstall csv) and then running npm install csv again.



+1


source


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

Instead of installing the package directly csv

as a dependency, install csv-parse

. If you still need the package csv

, install both.

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

      

This is required if you want to use the Synchronous API viarequire('csv-parse/lib/sync')

0


source







All Articles