1

I am trying to read XML file by browsing in local machine using input.

Here is my code

import React from "react";
import "./App.css";
import * as fs from "fs";
var parseString = require("xml2js").parseString;
const fileToParse =
  "file:///Users/apple/Documents/Project assignments/NewCCD/Ahmed.xml";
function App() {
  const getPath = (e) => {
    fs.readFile(fileToParse, function (err, data) {
      parseString(data, function (err, result) {
        console.log(result);
      });
    });
  };

return (
    <div>
      <input type="file" title="Browse" onChange={getPath} />
    </div>
  );
}

Here I am using the exact path still couldn't able to read file and getting this error.

TypeError: fs__WEBPACK_IMPORTED_MODULE_3__.readFile is not a function

Sayyam abbasi
  • 109
  • 1
  • 8

1 Answers1

0

fs can be used in a Node environment, not in a client React app. You could use a webpack loader to read xml files (like xml-loader for instance).

pierre
  • 11
  • 2