How to migrate from pdfjs-dist 3.11.174 to version 4 on a node.js project #17989
Replies: 13 comments 12 replies
-
Thanks for moving to correct position |
Beta Was this translation helpful? Give feedback.
-
Can nobody help? |
Beta Was this translation helpful? Give feedback.
-
You need to set |
Beta Was this translation helpful? Give feedback.
-
I also want to ask, do I have to use the old version if I want to use it in CommonJs |
Beta Was this translation helpful? Give feedback.
-
If not possible, I would like to know the latest version that supports commonjs |
Beta Was this translation helpful? Give feedback.
-
I have the same error when trying to upgrade pdfjs. But my porject is a bit different. I'm using So I tried to use a dynamic async And that is not surprising since this I think pdfjs is the only package we use that does not export a commonjs build, and I can't find a way to make it work properly. Any idea? |
Beta Was this translation helpful? Give feedback.
-
Thanks to a friend with knowledge in javascript jungle, I have finally a working version. My project is a streamlit application but you can find the useful information in the repository, https://github.com/lfoppiano/streamlit-pdf-viewer/tree/enable-text-content-2 in particular the important changes: lfoppiano/streamlit-pdf-viewer@5f09846 |
Beta Was this translation helpful? Give feedback.
-
I have the same problem. I was using 3.11.174 version, but it have audit problem, so I need patched 4.x.x version. Does anyone have a solution? |
Beta Was this translation helpful? Give feedback.
-
Solved. For those who still getting the error, here's my solution: tsconfig: {
"compilerOptions": {
"module": "NodeNext",
"esModuleInterop": true,
"removeComments": true,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "NodeNext",
"importHelpers": true,
"useDefineForClassFields": false,
"target": "ES2022",
"outDir": "./dist",
"allowJs": true,
"baseUrl": "./",
"incremental": true,
"lib": ["dom"]
},
"include": ["**/*.ts"],
"exclude": ["src/**/*.spec.ts", "./node_modules", "./dist"]
} my import: // import some type
import type { PDFDocumentProxy } from "pdfjs-dist";
// dynamic import
async function importPdfLib() {
const pdfjsLib = await import("pdfjs-dist/legacy/build/pdf.mjs");
return pdfjsLib;
} |
Beta Was this translation helpful? Give feedback.
-
我有同样的问题,项目是vue2,js应该怎么修改呢 |
Beta Was this translation helpful? Give feedback.
-
This fixed my problem: TypeStrong/ts-node#1290 (comment) |
Beta Was this translation helpful? Give feedback.
-
Is there still a legacy build available for v4 without typescript and without |
Beta Was this translation helpful? Give feedback.
-
I actually now jumped from v3 to v5 directly and used node v22 and it worked again. Somehow v4 with old node version seems to cause the problems |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I hope someone can help me with this issue.
I am using pdfjs-dist version 3.11.174 without any problems in my node.js 20.11.0 typescript project ("target": "esnext", "module": "commonjs", "moduleResolution": "node",)
Now I wanted to upgrade to version 4. But there are only mjs file now.
The problem is that even the documentation for node.js uses
But when I try to do it this way I get an error from typescript:
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/pdfjs-dist/build/pdf.mjs not supported.
Instead change the require of .../node_modules/pdfjs-dist/build/pdf.mjs to a dynamic import() which is available in all CommonJS modules.
So then I tried that:
import('pdfjs-dist/legacy/build/pdf.mjs').then(pdfjsLib => {
const { getDocument } = pdfjsLib;
// use getDocument here
}).catch(error => {
console.error('Error loading the module', error);
});
But even then I get nearly the same error message. What do I need to change? Is there somewhere a migration document that explains how to migrate from 3 to 4 for a node.js project?
Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions