Next.js Discord

Discord Forum

Deployed route handler doesn't work (Works on my machine... xD)

Unanswered
Red imported fire ant posted this in #help-forum
Open in Discord
Red imported fire antOP
I have a route handler that accepts a pdf file, extract it's content with pdf2md library, does some formatting and returns a json object.

In local development, everything works just fine.

I have deployed my app with Vercel. Whenever I try to use this api when it's deployed, I get following error message:

The \"paths[0]\" argument must be of type string. Received type number (78212)

Any idea why this would work locally but generate this message when it's deployed with Vercel?

Here is also a stripped down version of my route handler:

export async function POST(request: NextRequest) {
  try {
    // some other code...

    const nodeReadable = toNodeReadable(request.body)
    const bodyBuffer = await buffer(nodeReadable)
    const text = await pdf2md(bodyBuffer)

    // some other code...
    
    return NextResponse.json({
      success: true,
      message: formattedData,
    })
  } catch (error) {
    let errorMessage = 'Unknown error'
    if (error instanceof Error) {
      errorMessage = error.message
    }
    return NextResponse.json(
      {
        success: false,
        message: 'Conversion failed',
        error: errorMessage,
      },
      { status: 500 },
    )
  }
}

5 Replies

Fire ant
You are receiving path as type number, you need to treated it as a string
Red imported fire antOP
but why would that be a runtime error, only with the built version?

it seems that this error happens in pdf2md library and others using next.js have the same issue: https://github.com/opengovsg/pdf2md/issues/69

Are route handler serverless functions, ephemeral file systems or something fancy like that, which could be the issue?
Red imported fire antOP
Adding some images from my debugging session. On dev-example, is the working example. npm run dev

build-example is the code running after I ran npm run build && npm run start . It's exactly the same, but this generates the error...
Red imported fire antOP
The issue happens in the library code. Here some snippets:

The error gets triggered here:
const fontDataPath = path.join( path.resolve(require.resolve('pdfjs-dist'), '../../standard_fonts'), '/')


From this function:
const path = require('path')
const pdfjs = require('pdfjs-dist/legacy/build/pdf')
pdfjs.GlobalWorkerOptions.workerSrc = `pdfjs-dist/legacy/build/pdf.worker`

const { findPageNumbers, findFirstPage, removePageNumber } = require('../../lib/util/page-number-functions')
const TextItem = require('../models/TextItem')
const Page = require('../models/Page')

const NO_OP = () => {}

exports.parse = async function parse (docOptions, callbacks) {
  const { metadataParsed, pageParsed, fontParsed, documentParsed } = {
    metadataParsed: NO_OP,
    pageParsed: NO_OP,
    fontParsed: NO_OP,
    documentParsed: NO_OP,
    ...(callbacks || {}),
  }
  const fontDataPath = path.join( path.resolve(require.resolve('pdfjs-dist'), '../../standard_fonts'), '/')
  const pdfDocument = await pdfjs.getDocument(
    {
      data: docOptions,
      standardFontDataUrl: fontDataPath
    }).promise
  const metadata = await pdfDocument.getMetadata()
  metadataParsed(metadata)

  const pages = [...Array(pdfDocument.numPages).keys()].map(
    index => new Page({ index })
  )

  documentParsed(pdfDocument, pages)

// ...



Any idea what it could be?
Red imported fire antOP
I also create an express api, deployed it, and there the code works just fine. It seems to be something related with what is included/excluded in the next build version. but I don't get it.... :/