adaltas / node-csv-parse

CSV parsing implementing the Node.js `stream.Transform` API
https://csv.js.org/parse/
804 stars 166 forks source link

Upgraded to Angular 8 and module now breaks app. #288

Closed etavener closed 4 years ago

etavener commented 4 years ago

I have just upgraded my application from Angular 5 to Angular 8. Since this was done this module seems to causing a runtime error. Previously It had been working fine.

I can confirm that without this module the app runs as expected.

When I import the npm module as follows:

import * as csvParser from 'csv-parse/lib/sync';
...
this.injectedBody = this.convertToTable(csvParser(file));

I get the following console error:

Uncaught TypeError: Class extends value undefined is not a constructor or null at Object.8onQ

Using csv-parse@4.8.8

Any solutions either long term / short term fixes would be appreciated.

wdavidw commented 4 years ago

Which version did you use before? It is hard to help you with the little of information you provide. What does file stands for? This will be categorized into question until a bug is reported.

Luvideria commented 4 years ago

I have the same issue, except that for me it never worked. image

I am pretty new so tell me what info you need. I am using typescript btw. the example below works when we omit csvparser related code, it return the content of the file (but as one big string, obviously)

import * as CsvParser from 'csv-parse';
import {Observable, Subscriber} from "rxjs";
  function parse(file:File):Observable<any>{
    let fileReader = new FileReader();
    let p:CsvParser.Parser= CsvParser({delimiter:","});
    fileReader.readAsText(file);
    return Observable.create( (observer:Subscriber<any>):void=>{
       fileReader.onload = (e):void => {
        let res:string=fileReader.result as string
        p.write(res);
        let output:any=p.read();
      observer.next( output );
      }
      fileReader.onerror= (e):void =>{
        observer.error(e)
      }

    })
  }

here is my package.json: https://pastebin.com/9QWnDgFZ

Luvideria commented 4 years ago

The issue seems to be with the use of stream in browser context as explained here: https://github.com/mholt/PapaParse/issues/517

"paths": { "stream": [ "../node_modules/readable-stream" ] },

with npm install readable-stream in tsconfig.json solves part of the problem : the app can now load without the previous error.

Now I get this: image

But probably the fix would be to do as here: https://github.com/mholt/PapaParse/pull/580

wdavidw commented 4 years ago

I personally use this package with webpack and I didn't yet encounter any issue. I am not familiar with angular, are you using something like webpack or browserify?

Luvideria commented 4 years ago

Webpack ? Browserify? I have no idea what those are... Maybe looking at my package.json helps? I'm using angular typescript with angular material, and really, my app has no fancy options yet that could interfere.

wdavidw commented 4 years ago

This is probably the reason then. This package is written for Node.js which has a few module a web environment doesn't have, including your errors above, the stream module and process. A lot of web developer use a JavaScript bundler for performance and convenience. Among other things, the bundler will ensure the dependencies which are Node.js specific are satisfied.

wdavidw commented 4 years ago

Instead of using the module present in the lib folder, you should use the ones present inside lib/es5, for exemple instead of import * as CsvParser from 'csv-parse';, you should have something like import * as CsvParser from 'csv-parse/lib/es5';