csvToJson icon indicating copy to clipboard operation
csvToJson copied to clipboard

I receve "TypeError: fs.readFileSync is not a function"

Open mirandamatheusf opened this issue 1 year ago • 0 comments

Actual Behavior

TypeError: fs.readFileSync is not a function

I'm creating my application with Vite and I receive an error when trying to convert CSV to JSON

Steps to Reproduce the Problem

  1. yarn create vite (ReactJS + Typescript)
  2. the hook
const { acceptedFiles, getRootProps, getInputProps } = useDropzone({
    accept: { 'text/csv': ['.csv'] },
    onDrop: files => {
      const file = files[0];
      const reader = new FileReader();
      reader.onload = () => {
        const json = csvToJson
          .fieldDelimiter(';')
          .getJsonFromCsv(reader.result as string);
        console.log(json);
      };
      reader.readAsText(file);
    },
  }); 
  1. The return when trying to upload
Uncaught TypeError: fs.readFileSync is not a function
  at FileUtils.readFile (convert-csv-to-json.js)

Specifications

  • Version: Node V20.9.0
"convert-csv-to-json": "^2.44.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-dropzone": "^14.2.3",
"vite": "^5.2.0"

Recommendation

https://vitejs.dev/guide/troubleshooting#module-externalized-for-browser-compatibility

mirandamatheusf avatar Apr 12 '24 19:04 mirandamatheusf