From 3e481d5a55cc6d4e3f3a12f85574993639e1b738 Mon Sep 17 00:00:00 2001 From: vakabunga Date: Sat, 14 Mar 2026 14:18:25 +0300 Subject: [PATCH] fix: lazy-load pdf-parse to prevent startup crash if module is missing --- backend/src/services/pdfToStatement.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/backend/src/services/pdfToStatement.ts b/backend/src/services/pdfToStatement.ts index 112348a..7353e6a 100644 --- a/backend/src/services/pdfToStatement.ts +++ b/backend/src/services/pdfToStatement.ts @@ -63,9 +63,15 @@ export function isPdfConversionError(r: unknown): r is PdfConversionError { ); } -// pdf-parse v1 default export: (buffer, options?) => Promise<{ text, numpages, info }> -// eslint-disable-next-line @typescript-eslint/no-require-imports -const pdfParse: (buf: Buffer) => Promise<{ text: string }> = require('pdf-parse'); +// Lazy-loaded so the app starts even if pdf-parse is missing from node_modules. +let _pdfParse: ((buf: Buffer) => Promise<{ text: string }>) | undefined; +function getPdfParse() { + if (!_pdfParse) { + // eslint-disable-next-line @typescript-eslint/no-require-imports + _pdfParse = require('pdf-parse') as (buf: Buffer) => Promise<{ text: string }>; + } + return _pdfParse; +} export async function convertPdfToStatement( buffer: Buffer, @@ -80,7 +86,7 @@ export async function convertPdfToStatement( let text: string; try { - const result = await pdfParse(buffer); + const result = await getPdfParse()(buffer); text = result.text || ''; } catch (err) { console.error('PDF extraction error:', err);