无法在nodejs中使用jquery

Unable to use jquery in nodejs

本文关键字:jquery nodejs      更新时间:2023-09-26

我是Javascript/jquery的新手。我正在编写一个简单的js文件,用于解析csv文件。

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.
   }
});

当我运行上面的代码时,我得到以下错误:

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]

有人能提供建议吗?谢谢

在Node.js领域中不能像那样使用jQuery。

您需要依赖fs模块来读取文件,并依赖csv库来实际解析数据,请参见示例:

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

不要忘记在需要npm install csv之前先运行它!