ng2-pdf-viewer icon indicating copy to clipboard operation
ng2-pdf-viewer copied to clipboard

- Error: Module not found: Error: Can't resolve 'canvas' in '\node_modules\pdfjs-dist\build'

Open aishwhensheds opened this issue 3 years ago • 15 comments

Hello,

I am using the ng2-pdf-viewer 9.0 and angular 13, while creating the build by using the command ng run app-nameserve-ssr, I am facing following issue.

./node_modules/pdfjs-dist/build/pdf.js:4574:29-46 - Error: Module not found: Error: Can't resolve 'canvas' in '\node_modules\pdfjs-dist\build''

Can you please help me!

Regards,

aishwhensheds avatar Jul 09 '22 18:07 aishwhensheds

same problem! Cannot use ssr due to that!

alex-dietz avatar Aug 14 '22 16:08 alex-dietz

Same here. This really is a pain 😬😬

JonasBruns avatar Aug 15 '22 12:08 JonasBruns

Hello everyone,

Do we really need ng2-pdf-viewer to be rendered by the server?

Maybe there are workarounds with hybrid rendering?

If someone have a solution, I'm all ears

Maybe it can be helpful:

  • https://github.com/angular/angular-cli/issues/14319#issuecomment-488312317
  • https://www.newline.co/@eigenjoy/angular-ssr-the-browser-is-not-the-server--2b398f60

ODAncona avatar Aug 17 '22 09:08 ODAncona

Hi there, thanks for your remarks! no, we don't need a pdf-viewer on the server at all, the problem is how to easily exclude it. Your link is a great resource, yet, it unfortunately doesn't solve this issue here, as the error is actually not in the angular app. The check probably would need to be inside of pdf.js or rather ng2-pdf-viewer would need to define the global variables, see https://github.com/angular/universal/blob/main/docs/gotchas.md .

Hence, Im currently trying to exclude the pdf viewer from the build for the server, but this requires a custom webpack config where I have ng2-pdf-viewer in ignorePlugins . This works, but destroys a lot the advantages of the optimized builder of angular and I will have to see how easy it is to maintain.

alex-dietz avatar Aug 17 '22 19:08 alex-dietz

After trying several workarounds and spending several days 😵‍💫, I might just have found a quick solution for the server side rendering issue, could you please test the following and let me know if this is working for you as well:

In your angular.json find the "server" part and append to the options this line:

"externalDependencies": ["canvas"]

So it should look similar to this:

{
       ...
        "server": {
          "builder": "@angular-devkit/build-angular:server",
          "options": {
            "outputPath": "dist/project-name/server",
            "main": "server.ts",
            "tsConfig": "tsconfig.server.json",
            "inlineStyleLanguage": "scss",
            "externalDependencies": ["canvas"]
          },
          "configurations": {
            ...
          }
          ...
        }
  ...
}

VadimDez avatar Aug 20 '22 21:08 VadimDez

Great solution, too bad that Angular doesn't provide this idea in their documentation for ssr! Thank you so much for the effort! Works great!

alex-dietz avatar Aug 22 '22 13:08 alex-dietz

It's not working for my part. I still get another errors. For instance, I get:

const userAgent = navigator.userAgent || "";
                              ^
ReferenceError: navigator is not defined

ODAncona avatar Sep 10 '22 20:09 ODAncona

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 12 '22 09:11 stale[bot]

It's not working for my part. I still get another errors. For instance, I get:

const userAgent = navigator.userAgent || "";
                              ^
ReferenceError: navigator is not defined

@ODAncona Did you find any solution to fix this error?

k-kraus-aracom avatar Nov 17 '22 11:11 k-kraus-aracom

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 16 '23 21:01 stale[bot]

@VadimDez noway! this fixed the problem! after many days ... noway ... it works!!!!!!!!

AnthonyNahas avatar Mar 06 '23 20:03 AnthonyNahas

I'm using gatsby to build a static website. After installing pdfjs and trying to build the website. I'm facing the same error. Can anyone suggest any fix?

NobinPegasus avatar May 02 '23 20:05 NobinPegasus

It's not working for my part. I still get another errors. For instance, I get:

const userAgent = navigator.userAgent || "";
                              ^
ReferenceError: navigator is not defined

@ODAncona Did you find any solution to fix this error?

No.... and you ?

ODAncona avatar Jul 29 '23 06:07 ODAncona

I'm using gatsby to build a static website. After installing pdfjs and trying to build the website. I'm facing the same error. Can anyone suggest any fix? hi @NobinPegasus you can try this out. inside the gatsby-node.js, copy and paste this

 exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
    if (stage === "build-html" || stage === "develop-html") {
      actions.setWebpackConfig({
        module: {
          rules: [
            {
              test: /pdfjs-dist/,
              use: loaders.null(),
            },
          ],
        },
      })
    }
  }

samcyn avatar Nov 15 '23 15:11 samcyn