[ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type Function at _addListener (events.js:180:11)

754 views Asked by At

Update : The issue was solved after a clean installation of NodeJS.

I am trying to create a PDF file downloader in NodeJS. This is my code

                const file = fs.createWriteStream(filePath);
                const sendReq = request.get(pdfUrl);
                sendReq.on('response', (response) => {
                    if (response.statusCode !== 200) {
                        reject()
                    }
                    sendReq.pipe(file);
                });   
                file.on('finish', () => {
                    file.close()
                    resolve()
                });
                sendReq.on('error', (err) => {
                    console.log(err)
                    fs.unlink(file);
                    reject()
                });

This is the error I'm getting :

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type Function
at _addListener (events.js:180:11)
at WriteStream.addListener (events.js:240:10)
at WriteStream.close (fs.js:2302:10)
at WriteStream.<anonymous> (/var/www/html/lottery-api/app/Helpers/DocumentHelper.js:25:30)
at WriteStream.emit (events.js:164:20)
at finishMaybe (_stream_writable.js:605:14)
at afterWrite (_stream_writable.js:456:3)
at onwrite (_stream_writable.js:446:7)
at fs.js:2246:5
at FSReqWrap.wrapper [as oncomplete] (fs.js:703:5)

I'm using ubuntu 16.04 with Node : v10.15.0

If I remove this code, there are no errors

file.on('finish', () => { file.close() resolve() });

I'm using pdfreader package to parse the saved pdf file. If remove above code I can't handle the callback properly and the pdf reader produces an error.

Below is the error :

(while reading XRef): Error: Invalid XRef stream XRefParseException

 data: 'An error occurred while parsing the PDF: InvalidPDFException',

What am I doing wrong?

The same code works fine in MacOS Mojave 10.14 with node v10.15.0.

0

There are 0 answers