Search code examples
csvfile-uploadnestjspapaparsefile-processing

CSV File Processing with Nestjs and Papa Parse


I am trying to process a CSV file in NestJS using Multer and Papa Parse. I do not want to store the file locally. I just want to parse CSV files to extract some information.

However, I am unable to process it, I have tried two different ways. In the first one, I passed the file buffer to Papa.parse function. However, I get the error: ReferenceError: FileReaderSync is not defined

@Post('1')
@UseInterceptors(
    FileInterceptor('file', {})
)
async uploadFile(@UploadedFile() file: Express.Multer.File ){
    const csvData = papa.parse(file.buffer, {
        header: false,
        worker: true,
        delimiter: ",",
        step: function (row){
            console.log("Row: ", row.data);
        }
      });
}

So tried calling the readFileSync() as shown below, but this time I got the error, ERROR [ExceptionsHandler] ENAMETOOLONG: name too long, open

@Post('2')
@UseInterceptors(
    FileInterceptor('file', {})
)
async uploadFile(@UploadedFile() file: Express.Multer.File ){
    const $file =   readFileSync(file.buffer);
    const csvData = papa.parse($file, {
        header: false,
        worker: true,
        delimiter: ",",
        step: function (row){
            console.log("Row: ", row.data);
        }
      });
}

will appreciate any help to resolve this issue.


Solution

  • As pointed out by @skink, the file buffer needs to be converted to stream before it can be used by papa parse.

    const { Readable } = require('stream');
    

    And updated the function, where converting the file.buffer to stream before calling parse()

    @Post('1')
    @UseInterceptors(
        FileInterceptor('file', {})
    )
    async uploadFile(@UploadedFile() file: Express.Multer.File ){
        const stream = Readable.from(file.buffer);
        const csvData = papa.parse(stream, {
            header: false,
            worker: true,
            delimiter: ",",
            step: function (row){
                console.log("Row: ", row.data);
            }
          });
    }