next.js: Import pdfjs-dist not working correctly
Link to the code that reproduces this issue
https://github.com/Luluno01/pdfjs-dist-import-reproducer
To Reproduce
- Start the application in development mode (
next dev
) - Open home page (
/
) - Got an error in dev server console “Attempted import error: ‘getDocument’ is not exported from ‘pdfjs-dist’ (imported as ‘pdfjs’).” and
getDocument
beingundefined
.
Current vs. Expected behavior
The ESM package pdfjs-dist
should be imported correctly. The actual outcome, however, is nothing will be imported – all exported objects are undefined
, including the default export.
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: win32
Arch: x64
Version: Windows 11 Pro
Binaries:
Node: 21.1.0
npm: N/A
Yarn: N/A
pnpm: N/A
Relevant Packages:
next: 14.0.3-canary.1
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
typescript: 5.1.3
Next.js Config:
output: N/A
Which area(s) are affected? (Select all that apply)
App Router, TypeScript (plugin, built-in types)
Additional context
Same problem with version “13.5.4” and version “13.0.0”.
About this issue
- Original URL
- State: closed
- Created 8 months ago
- Reactions: 4
- Comments: 54 (3 by maintainers)
Hi, there’re some bundling fixes are landed on the canary (14.0.5-canary.45) I tested against latest canary it works well now.
getDocument
is a valid function. Another thing to notice that you don’t need to remove.default
to get the full module importsawait import('pdfjs-dist')
@huozhi Any thoughts?
Okayyyy… Thank you for your reply. Sounds like I have to upgrade to 14.0.5+ later to be able to use pdfjs with less workaround.
I decided to follow a simple path, I downloaded the stable version from the official website. I put all the files in the
public
folder. Then I added this tag to my component:then adding code in useEffect:
Yes, you are right. And my use case is server-side PDF file processing.
Try importing like this:
You mean so far it ONLY works in pages router?