Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

structuredClone is not defined #14729

Closed
planetsLightningArrester opened this issue Mar 30, 2022 · 6 comments
Closed

structuredClone is not defined #14729

planetsLightningArrester opened this issue Mar 30, 2022 · 6 comments

Comments

@planetsLightningArrester
Copy link

planetsLightningArrester commented Mar 30, 2022

Attach (recommended) or Link to PDF file here:

Configuration:

  • Web browser and its version:
    Actually using Node.JS 16.13.0
  • Operating system and its version:
    Windows 10 64 bits
  • PDF.js version:
    2.13.216
  • Is a browser extension:
    No

Steps to reproduce the problem:

  1. Load a PDF using the code below:
const PDFJS = require('pdfjs-dist');
PDFJS.getDocument({url: "doc.pdf", password: "123"})
  1. Run the code using node

What is the expected behavior? (add screenshot)
No error should be reported

What went wrong? (add screenshot)
An error is reported

node_modules/pdfjs-dist/build/pdf.js:2180
      data: structuredClone(obj, transfers)
            ^

ReferenceError: structuredClone is not defined

Link to a viewer (if hosted on a site other than mozilla.github.io/pdf.js or as Firefox/Chrome extension):

Notes:
I was using PDF version 2.7.570 and I didn't have this issue. I saw that the polyfill for structuredClone was removed recently. I'd like to know if there's any workaround for my case. For now, I'm just downgrading the version that I use. Thank you!

@Snuffleupagus
Copy link
Collaborator

When using the PDF.js library in Node.js you must use a legacy-build, since otherwise many things (and not just structuredClose) simply won't work unfortunately.
Hence changing your import to instead use pdfjs-dist/legacy/build/pdf.js ought to work, note also the examples in https://github.com/mozilla/pdf.js/tree/master/examples/node

For now, I'm just downgrading the version that I use.

Note that that's never recommended, since anything except the latest version is explicitly unsupported.

@mmahalwy
Copy link

@Snuffleupagus thoughts on when this would work on non-legacy version?

@planetsLightningArrester
Copy link
Author

planetsLightningArrester commented Sep 18, 2022

Just FYI, structuredClone() is supported from Node 17 onwards.
I also had issues running tests with Jest, although Node was >17, so I had to update Jest too.

I'm using the following versions and now everything is working perfectly.

Node: 18.9.0
jest: 29.0.3
pdfjs-dist: 2.16.105

import { getDocument, PDFDocumentProxy } from 'pdfjs-dist';

...

let pdf: PDFDocumentProxy | undefined = await getDocument({url: noteFullPath, password: pass}).promise;

@michelem09
Copy link

michelem09 commented Oct 12, 2022

Just FYI, structuredClone() is supported from Node 17 onwards. I also had issues running tests with Jest, although Node was >17, so I had to update Jest too.

I'm using the following versions and now everything is working perfectly.

Node: 18.9.0 jest: 29.0.3 pdfjs-dist: 2.16.105

import { getDocument, PDFDocumentProxy } from 'pdfjs-dist';

...

let pdf: PDFDocumentProxy | undefined = await getDocument({url: noteFullPath, password: pass}).promise;

Importing it from legacy as suggested by @Snuffleupagus works:

import pdfjsLib from 'pdfjs-dist/legacy/build/pdf.js';

@philip-hub
Copy link

When using the PDF.js library in Node.js you must use a legacy-build, since otherwise many things (and not just structuredClose) simply won't work unfortunately. Hence changing your import to instead use pdfjs-dist/legacy/build/pdf.js ought to work, note also the examples in https://github.com/mozilla/pdf.js/tree/master/examples/node

For now, I'm just downgrading the version that I use.

Note that that's never recommended, since anything except the latest version is explicitly unsupported.

Hello I am unable to find the legacy version. Is it still available?

@doczoidberg
Copy link

import pdfjsLib from 'pdfjs-dist/legacy/build/pdf.js';

needs canvas which I don't have in node.js 16?

odys-z added a commit to odys-z/Anclient that referenced this issue Aug 19, 2023
solution: import pdfjsLib from 'pdfjs-dist/legacy/build/pdf.js'
mozilla/pdf.js#14729 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants