-
Notifications
You must be signed in to change notification settings - Fork 13
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
Can't resolve 'canvas' on next.js serverless app #54
Comments
@EnricoNapolitan did you find a solution? I'm encountering something similar with Gatsby. |
Thanks for your detailed bug report @EnricoNapolitan. Since many people experience this issue, I will try to find a solution myself. |
I think I've seen this come up with JSDOM before. JSDOM marks canvas as an optional dependency https://github.com/jsdom/jsdom/blob/master/package.json#L58 |
Hi @NicholasEllul, |
I've reproduced the bug locally. Will try to find a solution. |
My hypothesis that adding How would it affect our existing users? |
I've tried to add
Maybe it's my local Ubuntu issue only but we can't be sure that it won't happen with other OSes too. |
Just tested on MacOS and building succeeded for me. However, I agree we cant be sure about other environments. There seems to be a reason JSDOM makes canvas optional despite some functionality requiring it So if we make it mandatory, we may hit the same issues |
Thanks @NicholasEllul . If adding |
I have a more or less similar issue while trying to use isomorphic-dompurify with AWS cdk and esbuild, got following errors
|
@nnduc1994 Hi, did you resolve this error, please? |
Just ran into this in Gatsby, any updates? Also receiving the same "Module parse failed..." error if I try to use this package with |
I was also having this issue with SvelteKit using the Node adapter. Installing |
Still seeing this issue using gatsby and trying to use the isomorphic-dompurify in a template file |
Same for me here |
Noting that this does not work (for me at least) on Next 13 not even on Serverless but normal out of the box Next app |
This one is new. Do you use the experimental app/ folder? Could you please give me more info? |
@kkomelin yes that was on the experimental app/ folder using I'm happy to do more testing and even contribute to fixes if I know what could be going on |
Thank you @Antonio-Laguna. I don't think there is much we can do here at the |
Is this tracked at JSDOM level somehow though? This might be extremely simplistic but shouldn't dompurify leverage the DOM instead of JSDOM which is intended to be leveraged only at Node level? |
Usually the error message looks like this:
What's your error message? As for the server vs client code, modern JS frameworks flirt a lot with the server (SSR, Server Components, etc) and Next is not an exception. |
SolutionFor usage of DOMPurify in Next.js without the wrapper package
Mainly the code is this (this is a Server Component - if you aren't using the import DOMPurify from "dompurify";
import { JSDOM } from "jsdom";
export default function Home() {
return (
<div
dangerouslySetInnerHTML={{
__html: DOMPurify(new JSDOM("<!DOCTYPE html>").window).sanitize(
"<img src=x onerror=alert(1)//>"
),
}}
/>
);
} There is also some required Next.js configuration for this, to resolve the webpack bundling errors:
/** @type {import("next").NextConfig} */
module.exports = {
reactStrictMode: true,
+ webpack: (config) => {
+ config.externals = [...config.externals, "canvas", "jsdom"];
+ return config;
+ },
}; |
Thanks @karlhorky. As I already asked here cure53/DOMPurify#400 (comment), since |
Should be fine, try modifying the CodeSandbox demo to use your library instead of https://codesandbox.io/p/sandbox/lingering-river-j5fpi7?file=%2Fapp%2Fpage.tsx |
Just confirmed. Your webpack fix works for |
Holy Crap the next config fix worked for me 🥳 |
@kkomelin @karlhorky Can you put more details into this why webpack changes fix this issue ? |
Don't know too much about it, check out the details I know here: |
i still have this error when using react-pdf
Error Message
when I add "canvas"
I still didn't update it to the next 13.3 since when I run yarn dev on windows, it crashes and throws errors |
@abedshaaban I found a way to resolve this when using react-pdf in Next Js 13 (app directory)
In your next.config.js have this
Let me know if it doesn't work. Happy to help out. This took me waaaay too long to figure out. Hopefully it will help someone get there faster! :) |
@mephobic i had a solution similar to that However, when I try yours, I get an error when I refresh the page with the error of what do you think the problem is ? |
It's difficult to debug without seeing what your error is. Can you try |
I don't know if this is directly related as I don't deploy to a lambda, but I thought it might be worth a mention. I saw this exact same error when I upgraded from 0.18.0 to 1.6.0 on my NextJS 13 stack. I had to do two things. I had a direct dependency of DOMPurify 3.0.3. Removing that dependency entirely and downgrading to 0.18.0 resolved the issue for me. In case it matters, my yarn.lock now references DOMPurify 2.4.x |
This works like a charm! Thank you @Daggron |
For me, I'm working with Next.js 13 server component and this worked for me: Server Component
next.config.js:
Just need to install both |
Still valid in 1.8.0 My Next.js build warns not only about missing 'canvas', but actually 3 missing deps from js-dom already Any luck with those fixes? |
It works now by only adding this in your next.config.js: module.exports = { and then you can copy this example in your component https://konvajs.org/docs/react/Transformer.html works fine in : "next": "^13.4.20-canary.0" |
Whats Wrong with my code its giving me error Load failed 'use client' const PdfViewer = ({SelectedFileData}) => { <Document file={"https://ina.org.au/wp-content/uploads/2022/05/Placeholder-PDF-1.pdf"} onLoadSuccess={onDocumentLoadSuccess}> export default PdfViewer |
@MrUsmanDev why do you think it's related to this particular issue? |
After trying out a lot of the suggested fixes (but for Nuxt instead of Next) we eventually switched to the following npm package: After installing this package and following the instructions on the link, the builds succeeded again! :) |
Happy to hear @brandon-v-seeters. Thanks for sharing |
What's the problem
I built a next.js application with serveless option, but simply importing this library it throws this error:
I already tried to add canvas library but this doesn't fix the problem and it throws another error.
Step to reproduce
Add to
next.config.js
:Run
next build
Basic Info:
This issue could be related to #35 , but i don't use umijs.
The text was updated successfully, but these errors were encountered: