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

nodeUtil imports to point to correct file #299

Closed Mythie closed 1 year ago

Mythie commented 1 year ago

Update the nodeUtil imports to point to the base/shared/util.js file instead of the node.js native util package.

Resolves some bundling issues with next.js.

modesty commented 1 year ago

What's the bundling issue this PR trying to solve? base/shared/util.js is not an ES Module, can not be safely imported. It's part of lib and extends native node's utils. Besides, PR has errors when running 'npm run test': ''' node:internal/process/esm_loader:97 internalBinding('errors').triggerUncaughtException( ^

Error [ERR_MODULE_NOT_FOUND]: ... '''

danielwii commented 1 year ago

using in nextjs v13 occurs this error:

error - Error [ReferenceError]: nodeUtil is not defined
    at eval (eval at <anonymous> (webpack-internal:///(sc_server)/./node_modules/pdf2json/lib/pdf.js), <anonymous>:1235:1)
    at eval (webpack-internal:///(sc_server)/./node_modules/pdf2json/lib/pdf.js:79:1)
Mythie commented 1 year ago

So looking more into this it has to do with Next13 route handlers and their bundling being aimed more at edge functions.

Not an error with the library fortunately, I’ll close this out.

Nitsorn commented 1 year ago

@Mythie were you able to fix this elsewhere?

Mythie commented 1 year ago

@Mythie were you able to fix this elsewhere?

Hey, once I swapped from route handlers to normal API routes in Next.js everything worked. Main issue was things getting bundled for edge rather than node.

Nitsorn commented 1 year ago

@Mythie Thank you, trying the same! Why is edge runtime important here vs. node?

Mythie commented 1 year ago

@Mythie Thank you, trying the same! Why is edge runtime important here vs. node?

Because it's running on the edge it doesn't have access to traditional Node API's and thus can't bundle something like pdf2json. I'd suggest tossing whatever interacts with this package in an api/ route until Next.js sort out route handlers and bundling them for the Node runtime.