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
require is not defined - pdf is not rendered on Chrome #782
Comments
I have the same issue but my react-pdf version is 5.2.0 I just remember updating npm to the lts |
Same issue working fine in dev but gives error in production. Tested on chrome and edge. |
Exactly! It works in the local environment and the problem exists on production with chrome/edge browsers |
Can confirm we are experiencing the same issue on production ( development works fine ). Currently using yarn 1.22.4 with node 14.5.1 with create react app. |
I managed to solve this problem in the following way:
I used to use the Document from 'react-pdf/dist/esm/entry.parcel' but since I started having the required error I had to switch to that other way |
I've been following the suggested steps but i still get the error
Any other suggestion to overcome the current issue? |
Hi @andreareginato, the only way I know that is going to work is by going to your Side note: Will work ✅: Won't work 🛑: |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
As of for now |
@wojtekmaj any progress on this? :) |
Nope, still no idea what the fix could look like. I'm also not getting it in any of my personal projects. These are based on Webpack 5 though so maybe the issue is affecting Webpack 4 only? |
This solution worked for me |
import { Document, Page, pdfjs } from 'react-pdf/dist/esm/entry.webpack'
const url = `//cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js`
pdfjs.GlobalWorkerOptions.workerSrc = url work for me |
@LiHaoGit's solution worked for me. |
I've set up sample repos on my server here: https://projects.wojtekmaj.pl/react-pdf/webpack4/ URLs are self-explanatory I think. Looks like both built and dev code works for me with default entry. I'm using the latest React-PDF version here. Anyone can reproduce this issue on the pages above? |
Can you provide the source code? Also, how do you run these pages? |
I have the same problem using The solution suggested by @jorge-uproar or import from CDN works for me. |
This bug costed me half a day. Thank you for saving me out of my misery. |
v5.4.0 that has been released 3 days ago seems to have solve the issue for me. Today I have also upgraded (clean install of the dependencies) to Node v16 and npm v7, but do think the above release fixed it. In any case, build the app and deployed it on staging and did not had the error anymore. Note: I use the
Thanks a lot @wojtekmaj and @njleonzhang !!! |
This seems to be indeed fixed in v5.4.0. |
New release fixed it for me as well! |
Still happening with v6.2.0 |
Is there a reproducible example you can share @sm3sher? |
@sm3sher Are you copying pdf.worker.js to your project's output folder? After I upgraded to v6 I noticed that I'd completely missed that step when I got started while using v5 and adding it via the CDN option got things working again. |
I tried the first option and unfortunately it was not working with this solution:
The CDN option worked out but I have react-apps that run inside a local context without internet access. |
@wojtekmaj I am also getting the same error after upgraded from 5.7.2 to latest 6.2.0. I am running React 18.x. Using Its working on my localhost but on production it gives error: With 5.7.2 it was working fine. Any recommended solution or I should revert to old version? |
For now @LiHaoGit 's CDN solution worked for me, also with webpack5 import. |
I am also using react-pdf version "^6.2.0" and running React "^18.1.0". I tried to implement @LiHaoGit 's CDN solution, but it didnt work for me. It is not throwing any error on the console as well. Below is my code. Any help is highly appreciated. import React, { useState } from 'react'; const options = { export const PdfViewerTemplate = ({props}) => {
); |
Experiencing the same error with react-pdf 6.2.1 and CRA (webpack5). |
Does this issue need help fixing? I would like to give it a try? @andreareginato @wojtekmaj |
@rashadatjou Yes, if it's still reproducible then yes. We have a bunch of sample repositories in here to test React-PDF with CRA5, Webpack 4, Webpack 5 and others, but it seems it's not enough. |
should this ticket be reopened then? |
|
I am running react-pdf 6.2.2 and had this error. I got everything working with the following. I downloaded the pdf.worker.min.js from this link (https://cdn.jsdelivr.net/npm/pdfjs-dist@2.16.105/build/pdf.worker.min.js) and put it in my public directory named as (pdf.worker.min.2.16.105.js) so I am not dependent on a CDN. Hope this helps someone else! This library is awesome! |
Was trying to fix this issue, then decided I should see if someone else had it. Turns out from all of the folks here having the same issue - it's extremely hard to get a pdf document loaded with a single library on the browser using create-react-app. Only solution that worked for me is:
On react 17, react-scripts 4.0.3 |
Same issue for me, work around work with both but we expect the library should come with all the dependencies, @wojtekmaj so I guess this issue is still open, no? |
@dbouclier You're correct, but this issue is not reproducible in any of our existing samples, so I'll need one to look into this matter closely. |
@wojtekmaj I got this issue only with production build, everything was working locally. no sure the samples are using the same dependencies as me details :
How I can help ? do you need more details ? |
Yeah. I've found out that this issue doesn't happen with the dev env. But if you build it and run the prod build on localhost, you can see the issue if you don't set |
Seem issue with my project, "react-pdf": "^5.7.1" |
This bug only happens with create-react apps (I suspect it is something with how babel works with create-react-app). I was able to reproduce the bug on the sample @wojtekmaj You can go to my fork https://github.com/rashadatjou/react-pdf and when you build the |
please reopen the issue! |
@nocive this might help From the README: Standard instructions will also work with Create React App. Please note that in Create React App, you can copy Copy over |
I managed to solve this issue for both development and production My setup:
So what I did was to follow the advice of zwarrell and download the pdf.worker.min.js and rename it. See the comment link below: I put it in my public folder and I put it inside the src to be able to load it while developing. In order to have my PDF render properly I imported the following 2 styling files: |
Worked for me too. Thanks. |
My setup:
@wojtekmaj I suggest to add this to the doc. |
This final approach seemed to get the file into the build for me, but one tweak: didn't work for my typescript app, due to no declarations file. But simply importing the file was sufficient, then left the existing worker config from the examples: import "pdfjs-dist/build/pdf.worker.entry";
pdfjs.GlobalWorkerOptions.workerSrc = new URL(
'pdfjs-dist/build/pdf.worker.min.js',
import.meta.url,
).toString(); |
If anyone is still facing this issue I have followed this approach: #852 (comment) By using approach from @codeWriter6 above I resolved the issue in production but faced an annoying issue in the localhost: "Uncaught runtime errors: Worker was terminated". So to fix that as well I followed the link above. My setup: "react": "^16.8.6", The fix from @dmr07 from the link above that worked for me:
|
Before you start - checklist
Description
After updating the react-pdf library from 5.2.0 to 5.3.0 pdf stopped being rendered on Chrome. All I can see is: Failed to load PDF file.
On the google chrome console I can find:
Uncaught ReferenceError: require is not defined
This problem exists only on google chrome because everything works without any problem on Firefox 88.0.
Environment
The text was updated successfully, but these errors were encountered: