TypeError [ERR_INVALID_ARG_TYPE]: The "cb" argument must be of type function. Received undefined
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.
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")`
"fs.writeFileSync("./test/bnr_01.json", JSON.stringify(pdfData));" you can edit the code in this way.
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.
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
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 ","
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