You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found this issue when upgrading a project that was using jsdom in server pages when upgrading from @sveltejs/kit@1.0.0-next.401 to @sveltejs/kit@1.0.0-next.491 but I have also seen this in newer versions like @sveltejs/kit@1.0.0-next.501.
When loading a page where the +page.server.ts includes jsdom, it throws an error in the console about an optional dependency for canvas
ENOENT: no such file or directory, open '__vite-optional-peer-dep:canvas:jsdom'
Error: ENOENT: no such file or directory, open '__vite-optional-peer-dep:canvas:jsdom'
at Object.openSync (node:fs:585:3)
at Object.readFileSync (node:fs:453:35)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1122:18)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (./node_modules/jsdom/lib/jsdom/utils.js:158:18)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
Of note in the stacktrace is this line ./node_modules/jsdom/lib/jsdom/utils.js:158. Which looks like this
exports.Canvas=null;letcanvasInstalled=false;try{require.resolve('canvas');canvasInstalled=true;}catch(e){// canvas is not installed}if(canvasInstalled){////// 👇 This is the line that causes the error //////constCanvas=require('canvas');if(typeofCanvas.createCanvas==='function'){// In browserify, the require will succeed but return an empty objectexports.Canvas=Canvas;}}
I was unable to reproduce this error in vite alone, and before the upgrade it was not an issue.
Reproduction
This problem can be reproduced by
Creating a new sveltekit project
Adding the jsdom dependency
Adding an import to jsdom to a new +page.server.ts file
You can see that it returns a string like in the error
I RESOLVED CANVAS "__vite-optional-peer-dep:canvas:jsdom"
If you comment out the import jsdom... the error goes away and the page loads the code that mimics the code in the jsdom modules, but require.resolve throws the exception that jsdom expects and so the require is skipped and there is no issue.
@gtm-nayan Thank you for the link. I was able to reproduce the issue in vite using the ssr template suggested in that comment and it does seem to be an issue from going from vite 3.0 to 3.1. I have filed a ticket in the vite repo vitejs/vite#10255 (for those who might end up here).
Describe the bug
I found this issue when upgrading a project that was using
jsdom
in server pages when upgrading from@sveltejs/kit@1.0.0-next.401
to@sveltejs/kit@1.0.0-next.491
but I have also seen this in newer versions like@sveltejs/kit@1.0.0-next.501
.When loading a page where the
+page.server.ts
includesjsdom
, it throws an error in the console about an optional dependency forcanvas
Of note in the stacktrace is this line
./node_modules/jsdom/lib/jsdom/utils.js:158
. Which looks like thisI was unable to reproduce this error in vite alone, and before the upgrade it was not an issue.
Reproduction
This problem can be reproduced by
jsdom
dependencyjsdom
to a new+page.server.ts
fileI have a repo made with the above steps
In that repo, you can see the issue by running
npm run dev
If you replace the
require.resolve('canvas');
line in./node_modules/jsdom/lib/jsdom/utils.js:158
, with the belowYou can see that it returns a string like in the error
I RESOLVED CANVAS "__vite-optional-peer-dep:canvas:jsdom"
If you comment out the
import jsdom...
the error goes away and the page loads the code that mimics the code in thejsdom
modules, butrequire.resolve
throws the exception thatjsdom
expects and so therequire
is skipped and there is no issue.Logs
No response
System Info
Severity
blocking an upgrade
Additional Information
No response
The text was updated successfully, but these errors were encountered: