I'm trying to fetch and parse PDF file using the pdfjs-dist package in node runtime inside the Vercel Serverless Function.
Code (TypeScript):
import * as pdfjs from "pdfjs-dist/legacy/build/pdf";
...
const document = await pdfjs.getDocument(url).promise;
...
Locally, when running vercel dev everything works OK. But after the deployment, when accessing the remote endpoint, I get following error:
Error: Setting up fake worker failed: \"Cannot find module './pdf.worker.js'
Any idea what can be causing this difference between local Vercel execution and the deployed version?
I found solution:
1: After compiling, you need to manually change this line of code. Vercel supports specifying custom compilation steps in package.json:
"scripts": {
"now-build": "node build.js"
}
2: The build.js file is used to complete the above replacement:
const fs = require('fs');
const dir = 'node_modules/pdfjs-dist/es5/build/pdf.js';
const content = fs.readFileSync(dir, { encoding: 'utf-8' });
fs.writeFileSync(dir, content.replace('"./pdf.worker.js";', `__dirname + "/pdf.worker.js";`))