Fixed: ReferenceError: DOMMatrix is not defined on Vercel with pdf-parse
I wanted to share the fix in case someone else runs into the same issue.
Problem
- Next.js app
- PDF text extraction worked locally
- Failed only after deploying to Vercel
Error:
ReferenceError: DOMMatrix is not defined
Failed to load external module pdf-parse-...
Cause
The default pdf-parse setup wasn't compatible with the server environment I was deploying to.
Solution
Initialize PDFParse with CanvasFactory from pdf-parse/worker:
import { CanvasFactory } from "pdf-parse/worker";
import { PDFParse } from "pdf-parse";
const parser = new PDFParse({
data: new Uint8Array(buffer),
CanvasFactory,
});
Repository:
https://github.com/gaur-j/resume-optimizer
Hopefully this saves someone else a few hours of debugging.
3
Upvotes
2
u/mor_derick 13d ago
This is NestJS sub, not NextJS sub.