pdf2json icon indicating copy to clipboard operation
pdf2json copied to clipboard

TypeError [ERR_INVALID_ARG_TYPE]: The "cb" argument must be of type function. Received undefined

Open brentreeves opened this issue 3 years ago • 5 comments

MAC OS M1 Ventura 13.4.1

npm run test is happy: 11 input files 11 success 0 fail 0 warning [email protected] [https://github.com/modesty/pdf2json]: 479.797ms

./pdf/va/form : Expected: NO Exception, All Parsed OK

It takes 8 seconds to process all PDFs ...

but the json example is not:

`╰─⠠⠵ node bnr_01.js node:internal/validators:440 throw new ERR_INVALID_ARG_TYPE(name, 'Function', value); ^

TypeError [ERR_INVALID_ARG_TYPE]: The "cb" argument must be of type function. Received undefined at maybeCallback (node:fs:177:3) at Object.writeFile (node:fs:2272:14) at PDFParser. (file:///Users/bnr01a/Documents/data/projects_current/pdf2json/bnr_01.js:8:8) at PDFParser.emit (node:events:511:28) at #onPDFJSParseDataReady (file:///Users/bnr01a/Documents/data/projects_current/pdf2json/node_modules/pdf2json/pdfparser.js:53:9) at PDFJSClass. (file:///Users/bnr01a/Documents/data/projects_current/pdf2json/node_modules/pdf2json/pdfparser.js:69:77) at PDFJSClass.emit (node:events:511:28) at file:///Users/bnr01a/Documents/data/projects_current/pdf2json/node_modules/pdf2json/lib/pdf.js:221:38 at process.processTicksAndRejections (node:internal/process/task_queues:77:11) { code: 'ERR_INVALID_ARG_TYPE' }

Node.js v20.3.1

╭─~/Documents/data/projects_current/pdf2json on master✘✘✘ 23-07-26 10:43 ╰─⠠⠵ cat bnr_01.js import fs from "fs"; import PDFParser from "pdf2json";

const pdfParser = new PDFParser();

pdfParser.on("pdfParser_dataError", errData => console.error(errData.parserError) ); pdfParser.on("pdfParser_dataReady", pdfData => { fs.writeFile("./test/bnr_01.json", JSON.stringify(pdfData)); });

pdfParser.loadPDF("./bnr_01.pdf")`

brentreeves avatar Jul 26 '23 15:07 brentreeves

"fs.writeFileSync("./test/bnr_01.json", JSON.stringify(pdfData));" you can edit the code in this way.

Roxza avatar Oct 14 '23 12:10 Roxza

This is an error in the docs of this library. fs.writeFile expects a callback and because you are not passing it, it throws an error. To fix this issue you need to define your above function like this: fs.writeFile("./test/bnr_01.json", JSON.stringify(pdfData), (data) => { console.log(data)}) Replace your fs.writeFile line with the above and your code will work.

alimustafa-poder avatar Nov 18 '23 15:11 alimustafa-poder

this error coming when you forget a callback function . eg fs.unlink('./image.jpg') error fs.unlink('./imgae',(err)=>{console.log(err)}) This is right way

abancp avatar Nov 29 '23 13:11 abancp

For my case , cb argument not define was just a comma "," mistake when i was declaring my arguments in the fs.writeFile() check if you accedently put a dot instead of a comma ","

hussamishtiaq avatar Sep 21 '24 04:09 hussamishtiaq

In my case it occured when i tried to write file operation in using async/await insted of callback or promise , i forgot to use .promises with fs module

const fs = require("fs").promises; or await fs.promises.writeFile("todo.txt","Learn Node.js");

this works for me

NB:- like other folks mentioned above if you miss the call back function in fs module you are aslo gonna get this error

MohdFarhanP avatar Dec 01 '25 02:12 MohdFarhanP