react-pdf icon indicating copy to clipboard operation
react-pdf copied to clipboard

Unexpected token <

Open valentinfily opened this issue 9 years ago • 29 comments

Hi,

Thank you for maintaining this awesome package :)

I'm trying to use it in my React app but I get the following error in the console and the component is stuck on loading:

Uncaught SyntaxError: Unexpected token < http://project_server.dev/app_folder/d58d9dd3a5c5f2a8eafb.worker.js

I'm loading a base64 PDF served by my Laravel backend. I'm using webpack and everything seem properly set up. The base64 PDF seems file too.

Any idea what I'm doing wrong?

Many thanks

valentinfily avatar Aug 29 '17 12:08 valentinfily

Hey there! To me, it looks like ......worker.js file doesn't exist in your case, and you probably get some HTML error page instead, hence the < token at the beginning of the file. Please see README.md section of this repository (if you're still on version 1.x, you'll have to look in file history or see README.md from your node_modules folder) on instructions how to handle workers.

wojtekmaj avatar Aug 29 '17 12:08 wojtekmaj

Hey, thanks for the quick answer. I tried without the worker enabled and your sample PDF and I get a 'Failed to load PDF file.' without further info. :(

valentinfily avatar Aug 29 '17 13:08 valentinfily

Which version are you on?

In general, it's a bad idea to start the project without the worker. In version 2.x, I've put extra effort to make it as easy as possible to do properly, so you might consider an update.

"Failed to load PDF file" is just a message shown to the user. You might want to catch the errors on your own - you have several props for that, please refer to README.md (they are usually called on...Error, so it's easy to find them).

wojtekmaj avatar Aug 29 '17 14:08 wojtekmaj

Thank you, I am using the 2.0.0-beta.4

For now I found a solution for it to work without the worker by adding .bind(this) to onDocumentLoad

I think the worker problem might come from a routing problem from my Laravel set-up

<Document
          file={file}
          onLoadSuccess={this.onDocumentLoad.bind(this)}
          loading={this.renderLoader()}
        >

Many thanks for your help!

valentinfily avatar Aug 29 '17 14:08 valentinfily

No worries. Here to help if you need anything in the future :)

wojtekmaj avatar Aug 30 '17 09:08 wojtekmaj

Hey @potioventures, for your information - v2.0.0 (non-beta) just went live!

wojtekmaj avatar Sep 05 '17 21:09 wojtekmaj

Have the same problem, without worker all works fine

kolyasya avatar Oct 08 '17 09:10 kolyasya

Hey @Kolyasya. Do you mean unexpected token? Usually Uncaught SyntaxError: Unexpected token < happens when JS file is not really there and a fallback is made to index.html. That would mean your webpack build is failing. If I'm right, would you share the full error with me?

wojtekmaj avatar Oct 09 '17 08:10 wojtekmaj

@wojtekmaj Yes. I'm using your package in my Meteor application, so it doesn't have Webpack, but works the same way. Initially I imported your package like import { Document, Page } from 'react-pdf';

In this case I have an error:

pdf.worker.js:1 Uncaught SyntaxError: Unexpected token <
modules.js?hash=d4b95ce9e5ca5bde9c775e356d9cf88b678060db:115519 Uncaught TypeError: Cannot read property 'WorkerMessageHandler' of undefined
    at modules.js?hash=d4b95ce9e5ca5bde9c775e356d9cf88b678060db:115519
    at HTMLScriptElement.Util_loadScript.script.onload (modules.js?hash=d4b95ce9e5ca5bde9c775e356d9cf88b678060db:112760)
(anonymous) @ modules.js?hash=d4b95ce9e5ca5bde9c775e356d9cf88b678060db:115519
Util_loadScript.script.onload @ modules.js?hash=d4b95ce9e5ca5bde9c775e356d9cf88b678060db:112760

If I switch to import { Document, Page } from 'react-pdf/build/entry.noworker';

All works nice and as expected 👍

kolyasya avatar Oct 09 '17 09:10 kolyasya

My problem here I think (and potentially @Kolyasya) is to be able to specify the worker's location given we use a separate routing system. Any idea if this is something that we can define in webpack or directly in this brilliant package? :)

valentinfily avatar Oct 09 '17 10:10 valentinfily

Same problem, how to fix it?

againksy avatar Feb 09 '18 08:02 againksy

@againksy, check in Network tab of developer tools what is actually loaded as pdf.worker.js. Chances are, it's a HTML 404 file which Webpack tries to parse as JS and obviously fails.

Check instructions for handling workers in README.md closely. In case of any further questions write here!

wojtekmaj avatar Feb 19 '18 00:02 wojtekmaj

@wojtekmaj Thank you for this awesome module. @kolyasya I am using this in a meteor app as well. For anyone tackling this in Meteor here's what I did:

  1. Copy node_modules/pdfjs-dist/build/pdf.worker.min.js to public/pdf.worker.min.js
  2. import { Document, Page, setOptions } from 'react-pdf'
  3. setOptions({ workerSrc: '/pdf.worker.min.js', })

Confirm it's loading: image

mattblackdev avatar Apr 09 '18 14:04 mattblackdev

Hi. I'm getting the same "Uncaught SyntaxError: Unexpected token <" with Webpack. An inspection does take me to the top of my index.html file. I'm sure the worker isn't loading correctly. I went through the Readme for version 3.x (I tried installing @^4.0.0, but npm said it didn't exist; I'm using React 16.3.2.) I have the following line in my component — import { Document, Page } from 'react-pdf/dist/entry.webpack';.

Is this what you mean in your readme, or is there a loader I should be adding into webpack.config? Or maybe I'm not doing something with cMaps? I'm not really clear what I may be doing wrong, even if it is a worker. Thanks for the assist.

abelsj60 avatar May 05 '18 13:05 abelsj60

@mattblackdev thank you, it works!

kolyasya avatar May 09 '18 09:05 kolyasya

@abelsj60 4.0.0 doesn't exist just yet, but 4.0.0-beta does. I'm fairly confident about this one; you can check it out on React 16.3 and up.

wojtekmaj avatar May 12 '18 08:05 wojtekmaj

here is a little bit of my 2sense:

  1. i think this issue might exist in the first place because of local filesystem. here is even a warning: https://github.com/wojtekmaj/react-pdf/blob/df795acc8bfad201de222124085a8216bf4c7d3a/src/entry.js#L9
  2. i tried the solution @mattblackdev and at first it worked. i actually have no clue what the reason is, but it stopped working shortly after. also, from that point on my app breaks if i do import { setOption } from "react-pdf" with a hard to debug message: Uncaught ReferenceError: Invalid left-hand side in assignment
  3. i assume the error from 2. comes from setOptions being a UMD module.

i hope this information is helpful for anyone trying to figure out, what's going on - or in the further development of version 4.0. i will also proceed to investigate later in time.

PS.: Another fix i guess would be to follow @mattblackdev's solution but instead of step 2 or 3 to just load the worker from your public/index.html (not tested though).

mxmzb avatar Jul 10 '18 20:07 mxmzb

Hi, I'm getting the same Uncaught token < error. My pdf.worker.js is returning some HTML document. I'm using react 15.5.4 and react-pdf 3.0.x. I read all the comments above but still didn't got the clear picture. Can anyone please help me!

Link to codesandbox https://codesandbox.io/s/zen-wright-ed5jc.

jtibrewal09 avatar Aug 26 '19 13:08 jtibrewal09

@jtibrewal09 You're requesting pdf.worker.js from a location that doesn't exists and you're getting a HTML with 404 error page instead of JS file. App tries to parse HTML file as a JS file and throws this error.

You used Create-React-App, so you should follow Create-React-App instructions.

In React-PDF 3.x, you will have to use setOptions to set workerSrc instead. So final form would look like:

https://codesandbox.io/s/jovial-kirch-p95he

wojtekmaj avatar Aug 26 '19 13:08 wojtekmaj

Thank you so much @wojtekmaj for helping me out this quick.

jtibrewal09 avatar Aug 26 '19 15:08 jtibrewal09