Unable to use jquery in nodejs

I am newbie to Javascript/jquery. I am writing a simple js file that parses a csv file.

var jquery = require('jquery');

jquery.get('file.csv', function(data) {
   alert(data); // this is a line
   var tempArray = data.split(','); // array of data
   for(var i = 0; i < tempArray.length; i++)
   {
       console.log(tempArray[i]); // probably index 1 is your IPv6 address.
   }
});

When I run the code above, I get the following error:

jquery.get('file.csv', function(data) {
       ^

TypeError: jquery.get is not a function
    at Object.<anonymous> (/Users/ishabir1/Desktop/transloc/parser.js:3:8)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Function.Module.runMain (module.js:442:10)
    at startup (node.js:136:18)
    at node.js:966:3
[Finished in 0.1s with exit code 1]

Can someone please advise? Thanks!

Answers 1

  • You cannot use jQuery like that in the Node.js realm.

    You need to rely on the fs module for reading files, and on a csv library for actually parsing your data, see example:

    var fs = require('fs');
    var parse = require('csv').parse;
    
    var parser = parse(function(err, data){
      console.log(data);
    });
    
    fs.createReadStream('file.csv').pipe(parser);
    

    Don't forget to run npm install csv before requiring it!


Related Articles