0

I have a section of code in javascript that looks like this:

var fs = require('fs');

var subscriptions = []

var array = fs.readFileSync('optimal.csv', 'utf8').toString().split("\r");
for (i in array) {
    stock = array[i].split(':')[0];
    if (stock)
        subscriptions.push(stock)
}

When I say:

node .\realtime.js

it has no problem reading the file 'optional.csv'.

However, if I copy the same section of code to a react.js application built with create-react-app and try to run it, I get errors:

TypeError: fs.readFileSync is not a function

when I run it thus:

npm run start

var fs = require('fs');

// Grid data as an array of arrays
const quoteList = [];
var i = 0;
var stock;

var array = fs.readFileSync('optimal.csv').toString().split("\r");
    for (i in array) {
        stock = array[i].split(':')[0];
        if (stock)
            quoteList.push(stock)
    }
  • What is the correct way to read a .csv file from a purely react application?
Ivan
  • 7,448
  • 14
  • 69
  • 134
  • It's because `react` !== `node`. Node is working on the server side, when React on the client. – alexmac Aug 10 '17 at 22:50
  • Right, but I thought they were both javascript. Why doesn't something as simple as reading a file not work from both!?!! – Ivan Aug 10 '17 at 22:51
  • My guess is that node is a server side technology while react is client side. Still, reading files are needed from both. – Ivan Aug 10 '17 at 22:52
  • This answer might help you https://stackoverflow.com/questions/44769051/how-to-upload-and-read-csv-files-in-react-js – Max Millington Aug 10 '17 at 23:06

1 Answers1

1

fs is a module implemented by node.js which is not provided by javascript itself. Maybe other environments provide fs as well, but react is just a module like fs, rather than another environment, so it can not provide extra module

Another thing, react code run in browser, where you expect fs refer to? Read every user's file system?

If you want to read your local csv file. You can use some bundler like Webpack which can bundle csv file into your output Javascript file. If reading local csv is what you want, you can do some thing like

const csv =require('/path/to/csv')

Maybe you need some plugin, because create-react-app may not provide the csv loader

Here are the steps to get a json from the csv file

  1. npm run eject in the project root
  2. npm install --save dsv-loader
  3. open webpack.config.dev.js under config
  4. under module>loaders>exclude(line 110) add /\.csv$/ in the list
  5. add {test: /\.csv$/,loader: 'dsv-loader'} entry in module>loaders
  6. re-run npm start
  7. require('path/to/csv') will return json
  8. do the same thing on webpack.config.prod.js if you need the production bundle
Guichi
  • 2,150
  • 1
  • 19
  • 27
  • Thanks. While this is helpful, it is insanely hard to read a text file from the local drive using react. I did as you suggest const csv =require('/path/to/csv'), but then how do I read the lines, one by one? – Ivan Aug 11 '17 at 02:13
  • @Ivan OK, I will figure it out for you, follow the instruction in the updated answer, you will get a json parsed from csv – Guichi Aug 11 '17 at 02:52