0

I have the following script (we'll call it myscript.js) which tries to parse the TSV file using D3.js:

var filename = "myfile.tsv";
//import d3.js
var d3 = require('/mypath/d3/d3.min.js').d3;
var tbl = d3.tsv(filename)

But why when I run it with the following command:

$ node myscript.js

It gave this error message:

/mypath/myscript.js:3
var tbl = d3.tsv(filename)
            ^
TypeError: Cannot read property 'tsv' of undefined
    at Object.<anonymous> (/mypath/myscript.js:32:13)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

What's the right way to do it?

neversaint
  • 60,904
  • 137
  • 310
  • 477
  • Well, After thinking for few minutes I came to know that why it's giving the error. The reason is in d3.min.js they are not exporting the d3 function/object, then how could we require that in our file. One more thing is we must not write .js extension while importing. Hope this will clear you. :D – saikiran.vsk Jul 07 '15 at 07:09
  • @sakiran.vsk: how can I make sure it export the d3 function/object? Can you give example? – neversaint Jul 07 '15 at 07:11
  • If they are exporting the d3 object/function, then we can see the line like exports.d3=function(){} or exports.d3=d3 or exports.d3=some object/variable. – saikiran.vsk Jul 07 '15 at 07:14
  • @saikiran.vsk: How can I modify my code to produce that? – neversaint Jul 07 '15 at 07:15
  • Does D3 in node even make sense? Isn't D3 a frontend graphics library, how would that run on node? – EJTH Jul 07 '15 at 08:21
  • @EJTH: I just need D3 function to parse CSV. – neversaint Jul 07 '15 at 08:26
  • Why not use papa parse instead? Or any other dedicated CSV library, why D3? There is a node.js fork of papa parse named baby parse: https://github.com/Rich-Harris/BabyParse – EJTH Jul 07 '15 at 08:26

0 Answers0