If you are using pdf-parse which is an excellent pdf-parsing library, you may run into a weird error:
Error: ENOENT: no such file or directory, open ‘./test/data/05-versions-space.pdf’
This error comes because of the following code in the npm module
let isDebugMode = !module.parent;
//process.env.AUTO_KENT_DEBUG
//for testing purpose
if (isDebugMode) {
let PDF_FILE = './test/data/05-versions-space.pdf';
let dataBuffer = Fs.readFileSync(PDF_FILE);
Pdf(dataBuffer).then(function(data) {
Fs.writeFileSync(`${PDF_FILE}.txt`, data.text, {
encoding: 'utf8',
flag: 'w'
});
debugger;
}).catch(function(err) {
debugger;
});
}
There are open pull requests, but the maintainer is not merging it. The simplest workaround, if you are using serverless is:
1. Copy /node_modules/pdf_parse/test/data/05-versions-space.pdf to the root of your project inside Root_Directory/test/data
2. Include it in your serverless
package:
include:
- test/**
That’s it! You are good to go.
but what in the production side of it?
On the production side, in my CI/CD job, I applied a patch in bash to replace this file inside node_modules with the one without the unnecessary code.