-
-
Notifications
You must be signed in to change notification settings - Fork 934
create-react-app setup yields Uncaught TypeError: Cannot read property 'GlobalWorkerOptions' of undefined
#291
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
Comments
From what I see you're using React-PDF 3.0.5. 4.0 which is on master branch will be out next week. Please see this :) --> https://github.com/wojtekmaj/react-pdf#before-you-continue TL;DR Use React-PDF 4.0 beta or use instructions for 3.x (not recommended) |
I am using My Component:
Also, there are lot of warning on console |
Hi @wojtekmaj Versions
My component imports import { Document, Page, pdfjs } from 'react-pdf' And my browser returns TypeError: Cannot read property 'GlobalWorkerOptions' of undefined on the following line pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js` I'm console logging Is there any chance the |
Any update on this issue? I'm having the same problem as @jmz7v |
This comment has been minimized.
This comment has been minimized.
Am having this same issue! PDFs not loading |
This comment has been minimized.
This comment has been minimized.
This is unrelated to this thread. see #280 for discussion on "Critical dependency: require function is used in a way in which dependencies cannot be statically extracted". |
Please kindly check React-PDF With these changes you should be able to use Webpack entry file in Create-React-App, without the need of using external CDN for hosting worker. Let me know what you think in #748! |
Hi, For anyone using react-rails and running into this problem. Check out this thread as well: I copied the worker.js to my components directory and fixed the webpacker errors as described of the link above. After that, the library is working well. |
import React from 'react';
import PropTypes from 'prop-types';
// react pdf viewer
import { PdfJs, Viewer, Worker } from '@react-pdf-viewer/core';
import '@react-pdf-viewer/core/lib/styles/index.css';
import { defaultLayoutPlugin } from '@react-pdf-viewer/default-layout';
import '@react-pdf-viewer/default-layout/lib/styles/index.css';
import { GlobalWorkerOptions } from 'pdfjs-dist';
import * as PDFworker from 'assets/pdf-worker-min.js'; // import from path to where file is stored
// Custom PDF viewer component
const PDFViewer = ({ pdfUrl }) => {
// Create new plugin instance
const defaultLayoutPluginInstance = defaultLayoutPlugin();
GlobalWorkerOptions.workerSrc = PDFworker
// Render the PDF viewer component
return (
<div style={{ height: '750px' }}>
{pdfUrl ? (
// Render the PDF viewer with the custom plugin instance "https://unpkg.com/[email protected]/build/pdf.worker.min.js"
<Worker>
<Viewer fileUrl={pdfUrl} plugins={[defaultLayoutPluginInstance]} />
</Worker>
) : (
<div>No PDF file selected</div>
)}
</div>
);
};
PDFViewer.propTypes ={
pdfUrl: PropTypes.string.isRequired,
}
export default PDFViewer;
|
Before you start - checklist
What are you trying to achieve? Please describe.
I would like to setup the pdf worker on an un-ejected create-react-app application
Describe solutions you've tried
First I tried using the instructions on Browserify and Others. This would give me the error in the title:
Uncaught TypeError: Cannot read property 'GlobalWorkerOptions' of undefined
.Then I tried importing pdfjs directly from
pdfjs-dist
, which got me:Uncaught TypeError: Cannot set property 'workerSrc' of undefined
Since both these options did not work, I tried following this comment, as such I got no errors, but the warnings below:
warning:
And as a matter of fact I don't even know if it worked (how can I check if there is a worker running?)
Environment
Thanks for any tips and pointers!
The text was updated successfully, but these errors were encountered: