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.

Gatsby ssr broken on netlify "Can't resolve 'canvas'"

See original GitHub issue

Hi i’m using the pdf viewer for create a website with Gatsby hosted on netlify but all my github actions for netlify come back failed.

  • Web browser and its version: Firefox dev edition version 89
  • Operating system and its version: macOS Big Sur version 11.2.3
  • PDF.js version: “version”: “2.5.207”,
  • Is a browser extension: no

i have read the log and found this

10:40:14 AM: Can't resolve 'canvas' in '/opt/build/repo/js/node_modules/pdfjs-dist/build'
10:40:14 AM: If you're trying to use a package make sure that 'canvas' is installed. If you're trying to use a local file make sure that the path is correct.
10:40:14 AM: not finished Caching JavaScript and CSS webpack compilation - 15.502s
10:40:14 AM: not finished Building HTML renderer - 15.441s
10:40:14 AM: not finished Caching HTML renderer compilation - 0.384s

I wonder if someone here could explain me what canvas is for thanks

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
LucaHermanncommented, Jul 15, 2021

@yordis Yeah i figured out this you just have to add inside a gatsby-node.js this code gonna block the loading of those package during the build because they dont have any window to refer to. When the site gonna be loaded and render to the dom they gonna load and have a window to refer so the error gonna be gone ! For every package who block you the ssr because of a window not defined just add this !

exports.onCreateWebpackConfig = ({ actions, stage, loaders }) => {
  actions.setWebpackConfig({
    devtool: "eval-source-map",
  })
  if (stage === "build-html") {
    actions.setWebpackConfig({
      module: {
        rules: [
          {
            test: /canvas/,
            use: loaders.null(),
          },
          {
            test: /pdfjs-dist/,
            use: loaders.null(),
          },
        ],
      },
    })
  }
}
1reaction
akando42commented, Aug 6, 2021

I have the same issue but with Next.JS deployment on Netlify. By adding next.config.js… it works for me.

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

Gatsby Site Can't Resolve - Netlify Support Forums
Hi @perry it looks like it's related to this issue, but I was able to fix it by clearing cache and retrying the...
Read more >
Gatsby Build Error while deploying - Netlify Support Forums
Hello I'm running to this issue on Netlify when it's executing the 'Gatsby Build' command, while on localhost everything is running smooth.
Read more >
Gatsby 5 upgrade error - Netlify Support Forums
The problem is that, our build system currently does not support build plugins with Node 18 and Gatsby 5 doesn't support Node 16....
Read more >
Run Gatsby 4 with DSG and SSR on Netlify today
It seems that Gatsby has broken free of its static-site generator (SSG) roots and is embracing server-side rendering. In addition to SSG, ...
Read more >
How to use Fabric in Gatsby? - Stack Overflow
It seems that fabric is using a global object (such as window or document ) to make their stuff. gatsby develop is compiled...
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