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!
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
I'm now learning NodeJS from nodeschool.io and the third exercise is about I/O file.
It's asking me to write a program using a single synchronous filesystem operation to read a file and print the number of newlines to the console (stdout). The full path to the file to read will be provided as the first command-line argument (i.e., process.argv[2]).
The answer for this exercise is similar to mine so I really know where I got wrong. This is my solution:
var fs = require('fs');
var contents = fs.readFileSync(process.argv[2]);
var strs = contents.toString();
var lines = strs.split('/n').length - 1;
console.log(lines);
but i got an error:
TypeError: path must be a string or Buffer
at Object.fs.openSync (fs.js:660:18)
at Object.fs.readFileSync (fs.js:565:33)
at Object.<anonymous> (D:\projects\dmt-node-study\first-io.js:3:19)
at Module._compile (internal/modules/cjs/loader.js:654:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
at Module.load (internal/modules/cjs/loader.js:566:32)
at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
at Function.Module._load (internal/modules/cjs/loader.js:498:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:695:10)
at startup (internal/bootstrap/node.js:201:19)
app.js
var fs = require('fs');
var contents = fs.readFileSync(process.argv[2]);
var strs = contents.toString();
var lines = strs.split('\n').length - 1;
console.log(lines);
Running the code from command line
>node app test.txt
Considering text file is in your project root directory.
A JSON file is 6 GB. When reading it with the following code,
var fs = require('fs');
var contents = fs.readFileSync('large_file.txt').toString();
It had the following error:
buffer.js:182
throw err;
^
RangeError: "size" argument must not be larger than 2147483647
at Function.Buffer.allocUnsafe (buffer.js:209:3)
at tryCreateBuffer (fs.js:530:21)
at Object.fs.readFileSync (fs.js:569:14)
at Object.<anonymous> (/home/readHugeFile.js:4:19)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)
Could somebody help, please?
The maximum size for a Buffer, which is what readFileSync() uses internally to hold the file data, is about 2GB (source: https://nodejs.org/api/buffer.html#buffer_buffer_kmaxlength).
You probably need a streaming JSON parser, like JSONStream, to process your file:
const JSONStream = require('JSONStream');
const fs = require('fs');
fs.createReadStream('large_file.json')
.pipe(JSONStream.parse('*'))
.on('data', entry => {
console.log('entry', entry);
});
u can read the file using line reader node js package and at every 50000 lines you can make small files squenceally then process those file and clear them out for your purpose if you have some task to read data from each line for a bigger file.line reader can do the job as it use stream in backend. the line reader dont wait for you if you directly read and process data like update in mongodb etc.
i did it and it worked even for a 10gb file .
I'm not sure if I'm gettin it right but I just started to use this jQuery-CSV parser plugin. I wanted to parse CSV file I have stored locally and then, based on results I get from it, use either jQuery's or ajax's notation to get certain data out of urls I provide.
var $ = jQuery = require('jQuery')
require ('./jquery.csv.js')
var sample = './analytics.csv';
fs.readFile(sample, 'UTF-8', function(err, csv) {
$.csv.toObjects(csv, {}, function(err, data) {
for(var i=0, len=data.length; i<len; i++) {
var link = toLink(data[i].Page)
$.get( link, function(data){
data[i].Category = $('.category').get(0)
});
console.log(data[i])
}
});
});
I'm getting back an error:
$.get(link, function (data){
^
TypeError: $.get is not a function
at main (E:\Programowanie\lolStats\index.js:16:7)
at Object.<anonymous> (E:\Programowanie\lolStats\index.js:37:1)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
It seems to me like it's looking for get() function in the jquery.csv.js file instead of regular jquery module I've installed for this project. What would be the easiest way to make both .csv.functions and regular jquery ones work in a same file?
I have the following tiny program.js which tries to execute a binary file:
var childProcess = require('child_process');
var path2Binary = '/home/myuser/myproj/bins/mybin';
var par = '--file=' + '/home/myuser/myproj/files/myfile.txt';
var ret = childProcess.execFileSync(path2Binary, [par]);
if (!ret) throw 'Error invoking process!';
var cnt = ret.stdout;
if (!cnt) throw 'Error retrieving output!';
console.log(cnt);
The program tries to execute a binary file and passes it a parameter (a file). The output of this process will be then displayed.
I try to run this: node program.js, but get the following
var ret = childProcess.execFileSync(path2Binary, [par]);
^
TypeError: Object #<Object> has no method 'execFileSync'
at Object.<anonymous> (/home/myuser/myproj/program.js:6:24)
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:929:3
More information
I am running on CentOS, Node version is v0.10.36.
I tried running sudo yum install nodejs, but it tells me it is already installed so Node installation looks kinda good.
What's the problem?
On a side note...
If I replace childProcess.execFileSync with childProcess.spawn I get the same.
If I change the first line into the following:
var exec = require('child_process').execFileSync;
Then I get an undefined exception on exec.
Synchronous child processes aren't supported in node v0.10.36 - https://nodejs.org/docs/v0.10.36/api/child_process.html
Looks like it may have been introduced in 0.12.
Alright, so I've created a test project to show off this error. The error being that Node JS can't find my getStr function in my Another object.
This is the code:
test.js
var Another = require('./another.js');
var Other = require('./other.js');
var otherStr = Other.getStr();
console.log(otherStr);
other.js
var Another = require('./another.js');
var str = Another.getStr();
another.js
var Other = require('./other.js');
var str = "other String";
exports.getStr = function(){
return str;
}
And this is my output:
C:\Users\Admin\Desktop\JS DEV\NODE DEV\server\test>node test.js
C:\Users\Admin\Desktop\JS DEV\NODE DEV\server\test\other.js:3
var str = Another.getStr();
^
TypeError: Object #<Object> has no method 'getStr'
at Object.<anonymous> (C:\Users\Admin\Desktop\JS DEV\NODE DEV\server\test\ot
her.js:3:19)
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 Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (C:\Users\Admin\Desktop\JS DEV\NODE DEV\server\test\an
other.js:1:75)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
C:\Users\Admin\Desktop\JS DEV\NODE DEV\server\test>
So how do I get Node JS to see Another's getStr function in Other?
What you're dealing with here is a circular dependency. Node.js will let you load modules in a circular way but you need to design your code to account for it. Generally speaking, a circular dependency is a sign that the design is suffering from some flaw. In the code you've shown in the question, another requires other but does nothing with it. So the simplest fix would be to change another so that it does not require other.
If you have to keep the circular dependency for some reason or you want to experiment with circular dependencies for learning purposes, then this would be another possible fix:
var str = "other String";
exports.getStr = function(){
return str;
}
var Other = require('./other');
// Actually do something with Other down here.
By the time other is required another will at least have getStr available. So this takes care of the immediate issue. Note however that your other module does not export anything so your test.js file will still fail at var otherStr = Other.getStr(); Probably you forgot to add this:
exports.getStr = function(){
return str;
}
(Note: I've modified the require call so that it requires other without the .js suffix. Generally, you don't want to put suffixes in your require calls. You want to put a module name which Node can resolve to a file, a package, or something else.)