Skip to content
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

Can't resolve 'canvas' in '(...)/node_modules/pdfjs-dist/build' #874

Closed
4 tasks done
cezarcarvalhaes opened this issue Oct 20, 2021 · 16 comments
Closed
4 tasks done
Labels
question Further information is requested stale

Comments

@cezarcarvalhaes
Copy link

cezarcarvalhaes commented Oct 20, 2021

Before you start - checklist

  • I followed instructions in documentation written for my React-PDF version
  • I have checked if this bug is not already reported
  • I have checked if an issue is not listed in Known issues
  • If I have a problem with PDF rendering, I checked if my PDF renders properly in Mozilla Firefox

Description
When building my Gatsby app, I get a module not found error: Error: Can't resolve 'canvas' in '(...)/node_modules/pdfjs-dist/build'

This is likely the same issue as #799 , however, that issue was closed and the issue and solution were Next.js-specific. I'm encountering this issue in Gatsby (v3).

Steps to reproduce

Steps to reproduce the behavior:

  1. Create a Gatsby App
  2. Install react-pdf
  3. Import react-pdf on a page and render a document.
  4. run gatsby build

Expected behavior

What you expected to happen?
The app to build.

Additional information
I was able to get around this by locking my react-pdf version at 5.2 as suggested in this comment from the #799 issue, however, this should not be a long-term solution. I encountered this error in version 5.4.1.

Environment

  • Browser (if applicable) [e.g. Chrome 57, Firefox 59]: N/A
  • React-PDF version [e.g. 3.0.4]: 5.4.1
  • React version [e.g. 16.3.0]: 17.0.2
  • Webpack version (if applicable) [e.g. 4.16.2]: 4.44.2
@transitive-bullshit
Copy link

This doesn't seem specific to Gatsby as I'm also getting the same error with Next.js 11.1.2 no matter how I try to import react-pdf (normal, dynamic, and dynamic w/ ssr disabled).

@wojtekmaj
Copy link
Owner

It makes no sense to use React-PDF server-side. Canvas contents can't be server side rendered.

@wojtekmaj wojtekmaj added the question Further information is requested label Oct 22, 2021
@cezarcarvalhaes
Copy link
Author

It makes no sense to use React-PDF server-side. Canvas contents can't be server side rendered.

Not sure if you were responding to me or the comment above, but just in case it was meant for me, this is not in a server side environment- it's strictly front-end.

@wojtekmaj
Copy link
Owner

wojtekmaj commented Oct 24, 2021

Gatsby and Next.js are both using a form of server side rendering, however they call it - it is done in Node.js.

@cezarcarvalhaes
Copy link
Author

Hmmm...I wonder what it is about version 5.2 that works without issue.

@wojtekmaj
Copy link
Owner

Are you able to provide full error stack, or a repro repo?

@jonavdd
Copy link

jonavdd commented Nov 2, 2021

Are you able to provide full error stack, or a repro repo?

The workaround to fix the version to v5.2.0 mentioned by @cezarcarvalhaes works for me to resolve the build part. But i encounter another issue. When using gatsby navigate to go to a page with the react-pdf on page its stuck in a loading state without any errors. When doing a hard refresh it works.

When using the latest version of react-pdf this navigation problem doesn't occur but the original error Can't resolve 'canvas' is back. So i'm kind of stuck.

@yordis
Copy link

yordis commented Nov 7, 2021

webpack(config, { isServer }) {
      // https://github.com/wojtekmaj/react-pdf/issues/799
      // https://github.com/mozilla/pdf.js/issues/13373
      if (isServer) {
        config.resolve.alias.canvas = false;
      }

      return config;
    },

That is how I solved the issue, canvas usage in the SSR should be avoided since it wouldn't make much sense

@plbrault
Copy link

plbrault commented Nov 7, 2021

I had the same error while using react-pdf on Gatsby, and I fixed it using that solution to an issue on another package:
react-pdf-viewer/react-pdf-viewer#497 (comment)

@cezarcarvalhaes
Copy link
Author

@wojtekmaj Sorry for the wait, here's a sample repo: https://github.com/cezarcarvalhaes/gatsby-react-pdf

It's failing in the SSR build, which makes sense. That must be why it works in development mode, but not in builds.

Also, a quick heads up, this same error throws in Gatsby 4: both in development and build.

@github-actions
Copy link
Contributor

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this issue will be closed in 14 days.

@github-actions github-actions bot added the stale label Feb 14, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Mar 7, 2022

This issue was closed because it has been stalled for 14 days with no activity.

@github-actions github-actions bot closed this as completed Mar 7, 2022
@syed-ahmed
Copy link

Leaving this here if people are integrating react-pdf with something like docusaurus. I took @yordis's solution and added a docusaurus plugin for react-pdf: https://github.com/syed-ahmed/docusaurus-plugin-react-pdf :). I was facing the problem mentioned in this issue before making this plugin. Since docusaurus exposes configuration of webpack using plugins, it made sense to encapsulate react-pdf as a plugin. The current plugin doesn't expose all the configuration that react-pdf provides and I don't have any plans right now to extend this, but contributions are welcome. My rendered pdfs look great, so thanks react-pdf authors!

image

@coltenkrauter
Copy link

This answer has a bit moe context: #799 (comment)

@NobinPegasus
Copy link

Having the same issue while building app using gatsby. After I install react-pdf
Can't apply #799 because I'm using gatsby.

@wojtekmaj
Copy link
Owner

Please kindly try react-pdf@7.0.0-beta.3 or later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested stale
Projects
None yet
Development

No branches or pull requests

9 participants