I am trying to execute a sample script that reads a CSV file. I copied the sample from this page:
http://www.adaltas.com/projects/node-csv/
I get this error:
csv()
^
TypeError: object is not a function
at Object.<anonymous> (/Users/paulchernoch/Documents/Chris Leung/read-csv-test.js:8:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
The sample code (modified to use my file names):
var csv = require('csv');
var fs = require('fs');
csv()
.from.stream(fs.createReadStream(__dirname+'/f1dcca4e8c5f76b3.csv'))
.to.path(__dirname+'/test.txt')
.transform( function(row){
return row;
})
.on('record', function(row,index){
console.log('#'+index+' ' /* +JSON.stringify(row) */);
})
.on('end', function(count){
console.log('Number of lines: '+count);
})
.on('error', function(error){
console.log(error.message);
});
I invoked the script from the command line like this:
> node read-csv-test.js
To verify whether I had the proper CSV module installed, I used the node package manager:
$ npm list
/Users/paulchernoch
├─┬ csv@0.4.0
│ ├── csv-generate@0.0.4
│ ├── csv-parse@0.0.3
│ ├── csv-stringify@0.0.2
│ └── stream-transform@0.0.2
├─┬ express@4.3.0
│ ├─┬ accepts@1.0.1
│ │ ├── mime@1.2.11
│ │ └── negotiator@0.4.3
│ ├── buffer-crc32@0.2.1
│ ├── cookie@0.1.2
│ ├── cookie-signature@1.0.3
│ ├── debug@0.8.1
│ ├── escape-html@1.0.1
│ ├── fresh@0.2.2
│ ├── merge-descriptors@0.0.2
│ ├── methods@1.0.0
│ ├── parseurl@1.0.1
│ ├── path-to-regexp@0.1.2
│ ├─┬ proxy-addr@1.0.0
│ │ └── ipaddr.js@0.1.2
│ ├── qs@0.6.6
│ ├── range-parser@1.0.0
│ ├─┬ send@0.3.0
│ │ ├── debug@0.8.0
│ │ └── mime@1.2.11
│ ├── serve-static@1.1.0
│ ├─┬ type-is@1.2.0
│ │ └── mime@1.2.11
│ └── utils-merge@1.0.0
├─┬ jade@1.3.1
│ ├── character-parser@1.2.0
│ ├── commander@2.1.0
│ ├─┬ constantinople@2.0.0
│ │ └─┬ uglify-js@2.4.13
│ │ ├── async@0.2.10
│ │ ├─┬ optimist@0.3.7
│ │ │ └── wordwrap@0.0.2
│ │ ├─┬ source-map@0.1.33
│ │ │ └── amdefine@0.1.0
│ │ └── uglify-to-browserify@1.0.2
│ ├── mkdirp@0.3.5
│ ├─┬ monocle@1.1.51
│ │ └─┬ readdirp@0.2.5
│ │ └─┬ minimatch@0.3.0
│ │ ├── lru-cache@2.5.0
│ │ └── sigmund@1.0.0
│ ├─┬ transformers@2.1.0
│ │ ├─┬ css@1.0.8
│ │ │ ├── css-parse@1.0.4
│ │ │ └── css-stringify@1.0.5
│ │ ├─┬ promise@2.0.0
│ │ │ └── is-promise@1.0.1
│ │ └─┬ uglify-js@2.2.5
│ │ ├─┬ optimist@0.3.7
│ │ │ └── wordwrap@0.0.2
│ │ └─┬ source-map@0.1.33
│ │ └── amdefine@0.1.0
│ └─┬ with@3.0.0
│ └─┬ uglify-js@2.4.13
│ ├── async@0.2.10
│ ├─┬ optimist@0.3.7
│ │ └── wordwrap@0.0.2
│ ├─┬ source-map@0.1.33
│ │ └── amdefine@0.1.0
│ └── uglify-to-browserify@1.0.2
├─┬ mariasql@0.1.20
│ └── lru-cache@2.3.1
└─┬ stylus@0.45.1
├── css-parse@1.7.0
├── debug@0.8.1
├─┬ glob@3.2.11
│ ├── inherits@2.0.1
│ └─┬ minimatch@0.3.0
│ ├── lru-cache@2.5.0
│ └── sigmund@1.0.0
├── mkdirp@0.3.5
└── sax@0.5.8
What am I missing? I am running MAC OS X 10.6.8. I installed node using Homebrew. I have successfully used node and javascript on Windows, but this is my very first attempt to use node on a MAC. (The ultimate goal is to parse CSV files and load them into MariaDb.)
Its not clear where you got your sample code, but it is probably no longer valid - a new version of the module ( 0.4
) was released recently with a very different API. You can either check out the new documentation at the project's GitHub page or install an older version by:
npm remove csv
npm install csv@0.3.7
0.3.7
has the API you appear to be working against, but you can of course go farther back if necessary.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.