zedjay72
zedjay72

Reputation: 177

Code works when running node console but not when invoking node <app_name.js>

I'm using:

npm install babyparse --save

when invoking

node

in Terminal (OS X Yosemite), I run the following commands and see output:

> var Baby = require('babyparse');
undefined
> var fs_test_data = Baby.parseFiles('fs_test.csv');
undefined
> var rows = fs_test_data.data;
undefined
>     rows.forEach(function(element, index, array){
...         console.log(element);
...         console.log(index);
...     });
[ '3000', '    1000', '    2000', '    30', '    0', '    1', '' ]
0
[ '3000', '    1000', '    2000', '    40', '    0', '    5', '' ]
1
undefined
> 

That's great! it works!

But....

//test_babyparse.js
var Baby = require('babyparse');
var fs_test_data = Baby.parseFiles('fs_test.csv');
var rows = fs_test_data.data;
rows.forEach(function(element, index, array){
    console.log(element);
    console.log(index);
});

and running

node test_babyparse.js

Ends up showing nothing. Why? Does this have to do with the IO and Event Loop?

Thanks,

Zakiir


Edit: I'm using the babyparse.js file found on github, NOT the .js file found on npm which does not have the parseFiles function.

Upvotes: 1

Views: 364

Answers (1)

zedjay72
zedjay72

Reputation: 177

phew!

so yes, I looked through the source of the babyparse.js file and noticed 'fs' was not defined.

I wrote

fs = require('fs');

in my test_babyparse.js, which I believe sets 'fs' in the global scope.

It worked!


Edit: Apparently it's not good practice to add things to global scope like that, so I added

var fs = require('fs');

in the babyparse.js file.

Upvotes: 1

Related Questions