0

Hi I have been attempting to convert a CSV to a JSON using csvtojson module. I have been trying to follow the steps outlined here but I am encountering an unusual error saying Uncaught Error: Cannot find module 'fs'

I am beginning by importing the module

import csv from "csvtojson";

I couldn't find any documentation on the csvtojson docs page stating that I should install and import a module fs (although I have attempted to do this).

The function I am running looks like this:

handleFiles = () => {
    let newfile = this.refs.file.files[0];
    let reader = new FileReader();

    csv({
        ignoreEmpty: true,
        headers: ["Year", "Month", "Name"]
    })
    .fromFile(newfile.name)
    .then((jsonObj) => {
        console.log(jsonObj);
    })
}

the input looks like this:

<input ref="file" type="file" onChange={this.handleFiles} filetypes={'.csv'}>
</input>

The only issue I can see is possibly from this line .fromFile(newfile.name)

The docs state to use .fromFile(<path to csv file>) but the path is not always known. Instead I am using the file name. Nevertheless the error I am getting doesn't seem to be related to this.

tdammon
  • 599
  • 1
  • 7
  • 26

1 Answers1

1

Problem

Uncaught Error: Cannot find module 'fs'

Answer

You are trying to use a library that has a Node server side library as a dependency. When the library runs it tries to use methods from the library fs which is used server side and is not used in React.js.

wuno
  • 9,547
  • 19
  • 96
  • 180