-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Use canvas in gatsby #17661
Comments
Gatsby is a ssr(server side framework), meaning that all the build process is done in node. With that won't have access to certain apis, like canvas. And it generates that error. As a workaround you could probably use react- loadable, for instance, let me see if I can make a reproduction of both packages and report back, do you mind waiting a bit? |
@jonniebigodes Yeah , thatswhy I used node-canvas , to bring canvas to SSR , but I could not make it to build the |
Hi! Sorry to hear you're running into an issue. To help us best begin debugging the underlying cause, it is incredibly helpful if you're able to create a minimal reproduction. This is a simplified example of the issue that makes it clear and obvious what the issue is and how we can begin to debug it. If you're up for it, we'd very much appreciate if you could provide a minimal reproduction and we'll be able to take another look. Thanks for using Gatsby! 💜 |
@LekoArts I translate the game and add it on a sandbox here : |
@jafar-rezaei i've cloned your codesandbox and it seems that i have a solution for you. Going to test out a couple of things and report back. |
Big thanks @jonniebigodes , the problem solved |
@jafar-rezaei no need to thank. Glad that i was able to solve your issue. |
@jafar-rezaei @jonniebigodes The canvas package is meant to work both in the browser and on Node.js, with the use of separate files for each platform. In our package.json, we've added a line to tell bundlers to use the file I thought that WebPack would automatically pick up on this 🤔 are you using any configuration that would make that not work? |
@LinusU i don't know how familiar you are with gatsby but all of the build is done on the node side of things, from my testing and based on the code @jafar-rezaei supplied the bundler/webpack configuration is the default that gatsby offers out of the box, with a small caveat and that is to ignore the canvas library during the build process, as outlined in these lines: exports.onCreateWebpackConfig = ({
stage,
rules,
loaders,
plugins,
actions,
}) => {
if (stage === "build-html") {
actions.setWebpackConfig({
module: {
rules: [
{
test: /canvas/,
use: loaders.null(),
},
],
},
})
}
}; It could be that the property |
Thank for this. As a side note we also had this issue during |
@larryg727 when this was written it was intended for production build with Gatsby, with that i ommited any configurations relevant to that. But nonetheless nice touch to include it and expand the response. |
@larryg727 you saved me a huge headache. Thank you |
I have an eraser effect with canvas on my Gatsby application. On the home page users can erase the canvas to see the video underneath the canvas. Everything works as normal on my local machine but when I serve the app, the canvas and video do not load initially. However, if I navigate to any page within the app and back to home page, the canvas and video load and work properly. I am new to Gatsby, please how can I resolve this. Please see below code:
|
i got the same error as @sayjeyhi and just like @larryg727 it happens during gatsby develop... @jonniebigodes so your approach is to mute the canvas plugin, did i understand this correctly? with
your are essentially doing the same as writing
but with canvas muted, an error is thrown in the browser now. i get a TypeError: Object(...) is not a function because this import: import { createCanvas } from 'canvas'; is importing nothing...which makes sense, since canvas is muted. but then again this is bad because i actually want to use it :D it would be great if someone could nudge me in the right direction how to fix this paradox... |
I am sorry, should've read the whole thread more thouroughly. I ended up with this Webpack config in gatsby-node.js which works just fine (the part with the resolve/alias is not relevant to this issue, the if-condition is the relevant thing):
|
weird but why does the proposed workaround not work for me?
anyone has an idea why this is still happening here? gatsby: |
Recently I was working on a project , which uses
node-canvas
,and it is working fine in development mode. but if I want to build package and
run gatsby build
canvas will got some error on last steps with message :Generating SSR bundle failed
I used
loadImage
function from node canvas in an async function like :and use it in some componenet to load image in react-konva :
There was a issue in
node-canvas
repo , which says add this configuration to webpack , and I did it , but it pass build level and make some errors on browser console :after build , we got this error on console :
My project is in this repo :
https://github.com/jafar-rezaei/snakeAndLadders.
File contents (if changed)
gatsby-config.js
: N/Apackage.json
: N/Agatsby-node.js
: N/Agatsby-browser.js
: N/Agatsby-ssr.js
: N/AThe text was updated successfully, but these errors were encountered: