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.

4
  • Are you writing a Node application? Commented Mar 31, 2019 at 17:43
  • no this is within a react component. Commented Mar 31, 2019 at 17:44
  • Sorry I linked the wrong answer, this is the right one, stackoverflow.com/a/48856076/2218253 It is likely it wont work since that is a node module and you are using React. Commented Mar 31, 2019 at 17:46
  • stackoverflow.com/a/48359480/2218253 Commented Mar 31, 2019 at 17:50

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.