wojtekmaj / react-pdf

Display PDFs in your React app as easily as if they were images.
https://projects.wojtekmaj.pl/react-pdf
MIT License
9.24k stars 877 forks source link

Error : build: Module not found: Can’t resolve ‘../build/Release/canvas.node’ #1592

Closed mz-chansm closed 11 months ago

mz-chansm commented 1 year ago

Before you start - checklist

Description

Two out of ten people had this error. I merged it into the development and installed it, but there was an error. The two are using m1 mac The error name is as follows. error :platform:build: Module not found: Can’t resolve ‘../build/Release/canvas.node’

As you told me, the setting is as follows.

next.config.js

module.exports = {
  reactStrictMode: true,
  transpilePackages: ['ui'],
  output: 'export',
  trailingSlash: true,
  webpack: (config) => {
    config.module.rules.push({
      test: /\.node/,
      use: {
        loader: 'raw-loader',
      },
    })

    return config
  },
}

package.json

  "dependencies": {
 "react-pdf": "^7.3.3",
},
  "devDependencies": {
    "raw-loader": "^4.0.2",
  }
}

pdf.tsx

'use client'
import { pdfjs, Document, Page } from 'react-pdf'
import 'react-pdf/dist/esm/Page/AnnotationLayer.css'
import 'react-pdf/dist/esm/Page/TextLayer.css'

import { File, OnPasswordCallback, PasswordResponse } from 'react-pdf/dist/cjs/shared/types'
import { useState } from 'react'

pdfjs.GlobalWorkerOptions.workerSrc = new URL('pdfjs-dist/build/pdf.worker.min.js', import.meta.url).toString()

type OnPassword = (callback: OnPasswordCallback, reason: PasswordResponse) => void
const PdfViewer = () => {
.../
  const [file, setFile] = useState<File>('/sample2.pdf')
     return (
    <>
      <div id="pdfWrap">
        <Document file={file} onLoadSuccess={onDocumentLoadSuccess} onPassword={onPassword}>
          <Page renderTextLayer={false} pageNumber={pageNumber} />
        </Document>
      </div>
      <p>
        Page {pageNumber} of {numPages}
      </p>
    </>
  )
}

Steps to reproduce

  1. Import PdfViwer from another component
  2. when pnpm build => error

Expected behavior

N/A

Actual behavior

N/A

Additional information

No response

Environment

jtaox commented 1 year ago

I have the same problem that's been solved here https://github.com/Automattic/node-canvas/issues/1825#issuecomment-1090125736

mz-chansm commented 1 year ago

I have the same problem that's been solved here Automattic/node-canvas#1825 (comment)

thank you why reason? only m1 mac issue ?

jtaox commented 1 year ago

I have the same problem that's been solved here Automattic/node-canvas#1825 (comment)

thank you why reason? only m1 mac issue ?

prebuilds node-canvas dependency is not compatible with ARM CPU.
I only encountered this issue on m1 mac.

mortocks commented 1 year ago

Does this mean there are potential deployment issues when using this library i.e. can you deploy to netlify vercel etc or do you need to create a more customised install environment?

wojtekmaj commented 11 months ago

I have just added functional Next.js samples in fc18d18132882e9a7bfb4d2d9a8cd16e8186bf32. Builds just fine on my M2 Mac. Check if these are working for you as well.

In the meantime, I'm closing this; if samples aren't working in some configurations, we will look at them instead.