node-csvtojson
node-csvtojson copied to clipboard
Error: Cannot find module 'fs'
Hello! I use npm only without webpack and other. I need to use csvtojson to convert csv in my app, but it dont works. I become this error:
Error: Cannot find module 'fs' ▶ 2 stack frames were collapsed. App.componentDidMount src/App.js:43 40 | componentDidMount() { 41 | 42 | // this.twoPies();
43 | csv() | ^ 44 | .fromFile(csvFilePath) 45 | .then((jsonObj)=>{ 46 | console.log(jsonObj);
its part of my code:
const csv = require('csvtojson'); const csvFilePath = '/data.csv'; /............/ csv() .fromFile(csvFilePath) .then((jsonObj)=>{ console.log(jsonObj); })
do you use the lib in node.js or in browser?
yes, of course i use node js v 8.10.0, app was created with create-react-app
Ok you are building a react app so you cannot use .fromFile
as you wont be able to access filesystem from a browser..
I'm getting that same error in a react app in electron where I do have access to fs any ideas?
Same issue here. The document about how to use csvtojson on browser is not very clear. Hope this can't be improved in the future.
the npm package Papaparse works well! I was getting the same error with csvtojson about .fromFile - you can combine FileReader with csv to JSON, or papaparse already has filereader built in. Still trying to format my data correctly but happy to post some code if anyone wants to see it!
Well what the heck, here's what I have so far, the csv data is logging correctly in the console as an object so off to a good start!
FYI don't necessarily need to run this in componentDidMount is using react...I intend to fetch the data in componentDidMount once we have it encrypted correctly, you might also use componentWillMount etc. class DataHandler extends Component { constructor(props) { super(props); this.state = { data: null }; //bind your callback method..... this.updateData = this.updateData.bind(this); }
componentDidMount() {
const Papa = require("papaparse");
const dataFilePath = require("../../data/myDataFile.csv");
Papa.parse(dataFilePath, {
download: true,
header: true,
//transformHeader: undefined,
delimiter: ",",
newline: ",",
//escapeChar: '"',
complete: this.updateData <-- (Papa.parse() does not return anything! you need a callback like below)
});
}
updateData(result) {
const data = result.data;
console.log(data);
I have a similar problem with the "Cannot find module 'fs'" error. I used csvtojson in browser by embedding the 'csvtojson.min.js' directly into the script tag, then the code: csv({ output: 'json' }) .fromFile(csvFilePath) .then(function(result){ console.log(result); })
gave the error of cannot find module 'fs'.
Guys, fs
is only available in a node environment, if you run javascript on a server, e.g. an express app.
Thats why the repo name is node-csvtojson.
If you use this library in a browser, fs
is not available.
The are other libraries for parsing a csv to json in javascript that runs a browser, e.g. papaparse.
Then why it says in the README it has support for browser?
For anyone else that ends up here looking for an in-browser example, this is what worked for me:
import {csv} from "csvtojson";
// ...
const res = await fetch(url);
const text = await res.text();
const jsonArray = await csv().fromString(text);
couple of notes:
- You may need to polyfill
fetch()
to support older browsers, or use something else to download the CSV and turn it into a string, there are lots of options: 'superagent', 'axios', 'jquery', etc. - Note the import is different from the current example in the README, if you
import * as csv
then you'll need to usecsv.csv()
because it imports an object, not the csv function
in browser you can read csv as text with FileReader and use fromString with result
const PromisifiedCsvToJson = (csvfile) => {
return new Promise ((resolve, reject) => {
var reader = new FileReader()
reader.readAsText(csvfile, 'UTF-8')
reader.onload = () => {
csv()
.fromString(reader.result)
.then((jsonArrayObj)=>{
resolve(jsonArrayObj)
})
}
})
}