Code Monkey home page Code Monkey logo

pdf-test's Introduction

PDF Test

This repository showcases a problem building a React App with an embedded React PDF Document component. In short, the PDF displays without any issues when running for local development, but doesn't display after the app has been built for production. It fails with the following error:

Uncaught ReferenceError: require is not defined
    at pdf.worker.min.347589700aee6bfb7b8a.js:1:21

This error is being thrown because the minified pdf.worker.js file produced by the build references hardcoded files from the hard drive of the machine that produced the build, and naturally the paths are broken when serving them statically.

Note: This app is heavily based on the create-react-app-5 sample provided by react-pdf.

Steps to reproduce:

First, verify that the app works fine when doing local development:

  • Make sure you are running Node 17.7.1
  • Open a terminal
  • Clone this repository
  • cd pdf-test
  • Run the app locally: npm i && npm start
  • Go to http://localhost:3000 in your browser
  • You should see the PDF document render correctly (it will say "This is a test PDF document")
  • Go back to your terminal and stop the npm process

Run the production build and see the failed error:

  • Run the production build: npm run build
  • Switch into the build directory: cd build/
  • Serve the static files locally on port 3000: python3 -m http.server 3000
  • Go to http://localhost:3000 in your browser
  • You will see an error saying that the PDF failed to load.
  • Open your browser console. You will see an error similar to the following: pdf.worker.min.347589700aee6bfb7b8a.js:1 Uncaught ReferenceError: require is not defined at pdf.worker.min.347589700aee6bfb7b8a.js:1:21
  • Go back to your terminal and stop the Python process

Examining the minified pdf.worker.min.js file:

  • In your favorite code editor, open the build/static/media/pdf.worker.min.<hash>.js file
  • Notice that all of the require statements reference absolute file paths from when they were built. For instance, in my minified JS file, I have require statements that look like this:
// Note: my local computer account is "administrator"
require("/Users/administrator/code/pdf-test/node_modules/@babel/runtime/helpers/defineProperty.js").default

Obviously, this is referencing the node_modules directory that is used for development. Since this is a production build, I would expect that the requires reference a static asset within the production build itself, or would be packed into the minified JS file.

What I have already tried

Additional Info

  • Node 17.7.1
  • React 18.2.0
  • React-PDF 7.7.1
  • Python 3.12.3
  • Chrome Version 124.0.6367.92 (Official Build) (x86_64)
  • MacOS 14.4.1 (intel)

pdf-test's People

Watchers

Joseph Cotton avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.