question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

Description

When building my Next JS application I get this error:

ModuleNotFoundError: Module not found: Error: Can't resolve 'canvas' in '(...)/node_modules/pdfjs-dist/build'

Steps to reproduce

  1. Create a Next JS application
  2. Install react-pdf
  3. Import react-pdf in some page
import { Document, Page, pdfjs } from "react-pdf/dist/umd/entry.webpack"
  1. Build (e.g. yarn build)

Expected behavior

Expected the application to build

Additional information

$ yarn build
yarn run v1.22.10
$ next build
(node:29765) [DEP0148] DeprecationWarning: Use of deprecated folder mapping "./" in the "exports" field module resolution of the package at (...)/node_modules/next/node_modules/postcss/package.json.
Update this package.json to use a subpath pattern like "./*".
(Use `node --trace-deprecation ...` to show where the warning was created)
Loaded env from (...)/.env.local
info  - Using external babel configuration from (...)/.babelrc
info  - Creating an optimized production build  
Failed to compile.

ModuleNotFoundError: Module not found: Error: Can't resolve 'canvas' in '(...)/node_modules/pdfjs-dist/build'


> Build error occurred
Error: > Build failed because of webpack errors
    at (...)/node_modules/next/dist/build/index.js:15:918
    at async (...)/node_modules/next/dist/build/tracer.js:1:1441
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Environment

  • React-PDF version: 5.3.0
  • React version: 17.0.1
  • Webpack version (if applicable): 4.44.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

5reactions
szczepanbarszczowskicommented, Jul 28, 2021

I managed to fix this issue with locking version of react pdf in package.json to "react-pdf": "5.2.0", previously it was "react-pdf": "^5.2.0", with this character ^ and it was causing resolution of pdfjs-dist to version 2.6. After change it results in 2.5 version which is not causing this problem.

5reactions
hfossli-agenscommented, Jun 21, 2021

Solution

Add this to .next.config.js:

module.exports = {
    target: "serverless",
    future: { webpack5: true },
    webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
        config.resolve.alias.canvas = false
        config.resolve.alias.encoding = false
        return config
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Looking for help to make npm/pdfjs-dist work with Webpack ...
This issue seems to arise due to esModule option introduced in worker-loader@3.0.0 . The fix for this was merged in (pre-release) ...
Read more >
Solved: Problem with pdfjs-dist on build - Alfresco Hub
We resolved using version specified in ADF application generator version 4.4.0. "pdfjs-dist": "2.5.207",. We started development using 4.3.0.
Read more >
pdfjs-dist - npm
Our goal is to create a general-purpose, web standards-based platform for parsing and rendering PDFs. This is a pre-built version of the PDF.js...
Read more >
How to Build a PDF Viewer with TypeScript & PDF.js | PDFTron
In this article, we show how to create a PDF viewing app using PDF.js and ... Navigate into the node_modules/@types/pdfjs-dist directory and add...
Read more >
canvas.node is not a valid win32 application - You.com
Install Cairo like installation process asked to (using msys ) · Install fabric · Write any code with fabric import · Build application...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found