node-csvtojson icon indicating copy to clipboard operation
node-csvtojson copied to clipboard

Error: Cannot find module 'fs'

Open dimakaplin opened this issue 5 years ago • 11 comments

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); })

dimakaplin avatar Oct 19 '18 12:10 dimakaplin

do you use the lib in node.js or in browser?

Keyang avatar Oct 19 '18 12:10 Keyang

yes, of course i use node js v 8.10.0, app was created with create-react-app

dimakaplin avatar Oct 19 '18 13:10 dimakaplin

Ok you are building a react app so you cannot use .fromFile as you wont be able to access filesystem from a browser..

Keyang avatar Oct 19 '18 13:10 Keyang

I'm getting that same error in a react app in electron where I do have access to fs any ideas?

dldugan14 avatar Dec 10 '18 22:12 dldugan14

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.

Graceliying82 avatar Jan 15 '19 16:01 Graceliying82

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);

Unionindesign avatar Feb 07 '19 18:02 Unionindesign

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'.

RushhourPark avatar Feb 07 '19 21:02 RushhourPark

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.

s-pic avatar Feb 08 '19 13:02 s-pic

Then why it says in the README it has support for browser?

dimslaev avatar Oct 30 '20 14:10 dimslaev

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 use csv.csv() because it imports an object, not the csv function

kriscarle avatar Nov 16 '20 20:11 kriscarle

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)
			})
		}
	})
}

777akot avatar Nov 03 '21 23:11 777akot