wojtekmaj / react-pdf

Display PDFs in your React app as easily as if they were images.
https://projects.wojtekmaj.pl/react-pdf
MIT License
9.46k stars 886 forks source link

Uncaught SyntaxError: Unexpected token '<' #1540

Closed schvenk closed 1 year ago

schvenk commented 1 year ago

Before you start - checklist

Description

I have been unable to get react-pdf working, even in a very simple sample app. (The sample app in the repo also doesn't work for me, though that's a different error.) Here's what I've tried.

  1. npx create-react-app pdftest
  2. Add a sample.pdf file inside pdftest/public. I've tried two of my own PDFs as well as the sample PDF from the repo.
  3. In App.js, remove all boilerplate code inside the outermost <div> and replace with <Document file="./sample.pdf" />
  4. yarn start
  5. Compiles and runs fine, but...
  6. In the browser I see:
Uncaught runtime errors:

ERROR
Uncaught SyntaxError: Unexpected token '<'
    at handleError (http://localhost:3000/static/js/bundle.js:57404:58)
    at http://localhost:3000/static/js/bundle.js:57423:7

ERROR
Unexpected token '<'
SyntaxError: Unexpected token '<'

Any idea what's going on?

Steps to reproduce

See above.

Expected behavior

PDF renders.

Actual behavior

Error as described above.

Additional information

No response

Environment

Sharoof-Khan commented 1 year ago

Same Issue

CRamos132 commented 1 year ago

I had the same issue, then after reading the rest of the README i came across this section then after adding this to my code it worked.

import { Document, Page, pdfjs } from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/legacy/build/pdf.worker.min.js`;
wojtekmaj commented 1 year ago

Duplicate of #994 Duplicate of #991 Duplicate of #680 Duplicate of #52

zsz-danzi commented 1 year ago

I have the same Issue.(Uncaught SyntaxError: Unexpected token '<' (at vendors-node_modules_pnpm_react-pdf_7_3_0_zula6vjvt3wdocc4mwcxqa6nzi_node_modules_react-pdf_d-e61ade.chunk.worker.js:1:)

now is resolve

import { Document, Page, pdfjs } from "react-pdf"; pdfjs.GlobalWorkerOptions.workerSrc = //cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js;