unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
6.06k stars 500 forks source link

Class extends value #<Object> is not a constructor or null using ws library with SSR with cloudflare preset #635

Closed vptill closed 1 year ago

vptill commented 1 year ago

Environment

Reproduction

https://github.com/vptill/not-a-constructor

Just create a new nuxt project, add SimpleDDP client as a plugin and build the application. It only fails with SSR and Cloudflare workers, SSG and dev work fine

Describe the bug

The application builds successfully, but the SSR server generation with cloudflare preset fails with the error: Class extends value # is not a constructor or null

Additional context

No response

Logs

TypeError: Class extends value #<Object> is not a constructor or null
    at \not-a-constructor\node_modules\ws\lib\websocket.js:44:25
    at \not-a-constructor\.nuxt\dist\server\_nuxt\error-500.65ff29b3.js:64:72
    at Script.runInContext (node:vm:139:12)
    at VMScriptRunner.runAsScript (\AppData\Roaming\nvm\v16.15.0\node_modules\miniflare\node_modules\@miniflare\runner-vm\src\index.ts:25:12)
    at VMScriptRunner.run (\AppData\Roaming\nvm\v16.15.0\node_modules\miniflare\node_modules\@miniflare\runner-vm\src\index.ts:84:12)
    at EventTarget.#reload (\AppData\Roaming\nvm\v16.15.0\node_modules\miniflare\node_modules\@miniflare\core\src\index.ts:775:42)
    at EventTarget.getPlugins (\AppData\Roaming\nvm\v16.15.0\node_modules\miniflare\node_modules\@miniflare\core\src\index.ts:1017:5)
    at createServer (\AppData\Roaming\nvm\v16.15.0\node_modules\miniflare\node_modules\@miniflare\http-server\src\index.ts:369:19)
    at startServer (\AppData\Roaming\nvm\v16.15.0\node_modules\miniflare\node_modules\@miniflare\http-server\src\index.ts:476:18)
    at main (\AppData\Roaming\nvm\v16.15.0\node_modules\miniflare\src\cli.ts:111:7)
vptill commented 1 year ago

Putting in nuxt.config.js alias: { ws: 'isomorphic-ws/browser.js' }

Solves the issue, however, it won't work on Cloudflare pages, because cloudflare has an unique way to handle websockets currently.