modesty / pdf2json

converts binary PDF to JSON and text, for server-side PDF processing and command-line use.
https://github.com/modesty/pdf2json
Other
2.02k stars 377 forks source link

ENOENT: no such file or directory - util.js #330

Closed FHoxhaDev closed 6 months ago

FHoxhaDev commented 9 months ago

I'm facing this issue below when using my application in vercel. It works fine on localhost but it doesnt on vercel.

Error:

Error: ENOENT: no such file or directory, open '/var/task/node_modules/pdf2json/base/shared/util.js'
    at Object.openSync (node:fs:596:3)
    at Object.readFileSync (node:fs:464:35)
    at /var/task/node_modules/pdf2json/pdfparser.cjs:1966:25
    at Array.reduce (<anonymous>)
    at Object.<anonymous> (/var/task/node_modules/pdf2json/pdfparser.cjs:1964:31)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at exports.b (/var/task/___vc/__launcher/chunk-BWVOFI3E.js:1:1211) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: '/var/task/node_modules/pdf2json/base/shared/util.js',
FHoxhaDev commented 8 months ago

Hi @modesty,

Is this an issue with Vercel or with the package itself? Is there any workaround to fix this error?

kylerob commented 8 months ago

It's an issue with Vercel and executing this code on the server side due to edge functions. The issue is all of these files, the first of which you're seeing an error for, can't be read on the edge function because it hasn't been copied over by Vercel's edge function setup code. Vercel usually does a good job including what you need, but that's if you're importing code via module imports. This code is building up a string of all those file contents and eval'ing it.

I have a fork with a semi-working version that creates the big import file in a separate step, and then imports it into the main files so Vercel includes everything. I'm working on fixing an issue with a canvas function that's defined in one file and used in another that's not working, but you can try it out if you want. If I get it working today I'll create a PR in the main repo to get it merged into the baseline.

kylerob commented 8 months ago

@FHoxhaDev Full fix is on this branch, try it out and let me know if it works for you:

"pdf2json": "github:kylerob/pdf2json#fix-createScratchCanvas-import"

It looked good in my tests.

JinhooBong commented 8 months ago

@kylerob by pointing my "pdf2json" in package.json will it point to your branch?

EDIT: it does resolve that issue. Thanks Kyle!

FHoxhaDev commented 8 months ago

Hi @kylerob ,

Thank you for your fix. Unfortunately, for me it didn't work. It is failing at my API call where it uploads the file, this is my endpoint:

https://gist.github.com/FHoxhaDev/59e2dc2b1cc134b6d064b96573a51145

and this is the error:

image

Locally it works but not on Vercel host.

kylerob commented 8 months ago

@FHoxhaDev Try forcing a redeploy on Vercel -- I ran into the same issue at first. Vercel seems to cache npm packages, because I've had trouble getting it to bring in my latest npm changes sometimes.

adpadillar commented 8 months ago

Hey @kylerob, thanks for your fix!

I just wanted to ask some quick questions.

  1. Any plans on making a PR with this fix at any point?
  2. If not, any plan to publish a separate npm package with your fork

I ask so we can install by doing a normal "pdf2json": "x.x.x" or even a "vercel-pdf2json": "x.x.x" instead of the current github thing

Cheers!

modesty commented 6 months ago

thanks to @kylerob. A more thorough fix plus appropriate tests are in PR #338, please review and test it out

kylerob commented 6 months ago

@modesty Thanks for bringing it home!

modesty commented 6 months ago

fixed in 3.1.2