简体   繁体   中英

FS module to read CSV file

I'm new with node js and I tried to use the module "fs" but I have always the same error that I can't understand: " Cannot find module 'fs' ". The code is:

     const fs = require('fs');
      fs.createReadStream("data/dataset.csv", "utf8", function(error, data)
        {
          data = d3.tsv.parse(data);
          console.log(JSON.stringify(data));
        }
      );

Can someone help me? Thank you

You're using fs wrong!

First of all you're not using stream properly.

Also you don't need stream for this task. (if you just need to read file)

Try this instead:

fs.readFile("data/dataset.csv", "utf8", (err, data) => {
  if (err) throw err;
  data = d3.tsv.parse(data);
  console.log(JSON.stringify(data));
});

By the assumption that you are using webpack I don't think that it is possible to require fs in the browser. Webpack take your code and bundle it, then it is "usually" sent to a browser. Although webpack is reading and using node.js code it will ultimately run in the browser which does not have the required 'fs' module and is not a node.js environment.

I am not sure what you are trying to achieve.

With webpack it is possible to change the configuration and have custom file loaded as a step. You should take a look at Adding a csv file to a webpack build

Or Webpack Express Cannot Resolve Module 'fs', Request Dependency is Expression

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM