Supabase + SvelteKit web app runs fine locally, builds and deploys OK, but crashes when site is visited

The Netlify app: https://stunning-eclair-2afe4c.netlify.app

The error:

An unhandled error in the function code triggered the following message:

Runtime.UnhandledPromiseRejection - Error: Dynamic require of "events" is not supported

Stack trace:

Runtime.UnhandledPromiseRejection: Error: Dynamic require of "events" is not supported
    at process.<anonymous> (file:///var/runtime/index.mjs:1131:17)
    at process.emit (node:events:527:28)
    at emit (node:internal/process/promises:140:20)
    at processPromiseRejections (node:internal/process/promises:274:27)
    at processTicksAndRejections (node:internal/process/task_queues:97:32)

This doesn’t happen in local dev, just on Netlify—and as I said, the build proceeds without an error. Confused what I’m doing wrong… The repo is public in case that helps you help me: https://bitbucket.org/r2-creative/gameonornot/src/main/

The function log:

Oct 19, 08:06:50 AM: 7a41a5c5 :41)","    at __require2 (file:///var/task/app/.netlify/functions-internal/render.mjs:47:50)","    at node_modules/.pnpm/websocket@1.0.34/node_modules/websocket/lib/WebSocketServer.js (file:///var/task/app/.netlify/functions-internal/render.mjs:7457:18)","    at __require2 (file:///var/task/app/.netlify/functions-internal/render.mjs:47:50)","    at node_modules/.pnpm/websocket@1.0.34/node_modules/websocket/lib/websocket.js (file:///var/task/app/.netlify/functions-internal/render.mjs:8445:17)","    at __require2 (file:///var/task/app/.netlify/functions-internal/render.mjs:47:50)","    at node_modules/.pnpm/websocket@1.0.34/node_modules/websocket/index.js (file:///var/task/app/.netlify/functions-internal/render.mjs:8461:22)","    at __require2 (file:///var/task/app/.netlify/functions-internal/render.mjs:47:50)","    at node_modules/.pnpm/@supabase+realtime-js@1.7.4/node_modules/@supabase/realtime-js/dist/main/RealtimeClient.js (file:///var/task/app/.netlify/functions-internal/render.mjs:8900:23)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: Dynamic require of \"events\" is not supported","    at process.<anonymous> (file:///var/runtime/index.mjs:1131:17)","    at process.emit (node:events:527:28)","    at emit (node:internal/process/promises:140:20)","    at processPromiseRejections (node:internal/process/promises:274:27)","    at processTicksAndRejections (node:internal/process/task_queues:97:32)"]}Oct 19, 08:06:50 AM: 7a41a5c5 [ERROR] [1666188411441] LAMBDA_RUNTIME Failed to get next invocation. Http Response code: 403Oct 19, 08:06:50 AM: 7a41a5c5 2022-10-19T14:06:51.880Z	undefined	ERROR	Unhandled Promise Rejection 	{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"Error: Dynamic require of \"events\" is not supported","reason":{"errorType":"Error","errorMessage":"Dynamic require of \"events\" is not supported","stack":["Error: Dynamic require of \"events\" is not supported","    at file:///var/task/app/.netlify/functions-internal/render.mjs:29:9","    at node_modules/.pnpm/websocket@1.0.34/node_modules/websocket/lib/utils.js (file:///var/task/app/.netlify/functions-internal/render.mjs:5950:41)","    at __require2 (file:///var/task/app/.netlify/functions-internal/render.mjs:47:50)","    at node_modules/.pnpm/websocket@1.0.34/node_modules/websocket/lib/WebSocketServer.js (file:///var/task/app/.netlify/functions-internal/render.mjs:7457:18)","    at __require2 (file:///var/task/app/.netlify/functions-internal/render.mjs:47:50)","    at node_modules/.pnpm/websocket@1.0.34/node_modules/websocket/lib/websocket.js (file:///var/task/app/.netlify/functions-internal/render.mjs:8445:17)","    at __require2 (file:///var/task/app/.netlify/functions-internal/render.mjs:47:50)","    at node_modules/.pnpm/websocket@1.0.34/node_modules/websocket/index.js (file:///var/task/app/.netlify/functions-internal/render.mjs:8461:22)","    at __require2 (file:///var/task/app/.netlify/functions-internal/render.mjs:47:50)","    at node_modules/.pnpm/@supabase+realtime-js@1.7.4/node_modules/@supabase/realtime-js/dist/main/RealtimeClient.js (file:///var/task/app/.netlify/functions-internal/render.mjs:8900:23)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: Dynamic require of \"events\" is not supported","    at process.<anonymous> (file:///var/runtime/index.mjs:1131:17)","    at process.emit (node:events:527:28)","    at emit (node:internal/process/promises:140:20)","    at processPromiseRejections (node:internal/process/promises:274:27)","    at processTicksAndRejections (node:internal/process/task_queues:97:32)"]}Oct 19, 08:06:50 AM: 7a41a5c5 [ERROR] [1666188411913] LAMBDA_RUNTIME Failed to get next invocation. Http Response code: 403Oct 19, 08:06:50 AM: 7a41a5c5 2022-10-19T14:06:51.913Z	undefined	INFO	Unexpected Top Level Error: Error: Failed to get next invocation, error 403Oct 19, 08:06:50 AM: 7a41a5c5 Unknown application error occurredOct 19, 08:06:50 AM: 7a41a5c5 Runtime.UnhandledPromiseRejectionOct 19, 08:06:51 AM: 7a41a5c5 Duration: 479.55 ms	Memory Usage: 48 MB	Oct 19, 08:38:39 AM: f4e42876 /render.mjs:29:9","    at node_modules/.pnpm/websocket@1.0.34/node_modules/websocket/lib/utils.js (file:///var/task/app/.netlify/functions-internal/render.mjs:5950:41)","    at __require2 (file:///var/task/app/.netlify/functions-internal/render.mjs:47:50)","    at node_modules/.pnpm/websocket@1.0.34/node_modules/websocket/lib/WebSocketServer.js (file:///var/task/app/.netlify/functions-internal/render.mjs:7457:18)","    at __require2 (file:///var/task/app/.netlify/functions-internal/render.mjs:47:50)","    at node_modules/.pnpm/websocket@1.0.34/node_modules/websocket/lib/websocket.js (file:///var/task/app/.netlify/functions-internal/render.mjs:8445:17)","    at __require2 (file:///var/task/app/.netlify/functions-internal/render.mjs:47:50)","    at node_modules/.pnpm/websocket@1.0.34/node_modules/websocket/index.js (file:///var/task/app/.netlify/functions-internal/render.mjs:8461:22)","    at __require2 (file:///var/task/app/.netlify/functions-internal/render.mjs:47:50)","    at node_modules/.pnpm/@supabase+realtime-js@1.7.4/node_modules/@supabase/realtime-js/dist/main/RealtimeClient.js (file:///var/task/app/.netlify/functions-internal/render.mjs:8900:23)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: Dynamic require of \"events\" is not supported","    at process.<anonymous> (file:///var/runtime/index.mjs:1131:17)","    at process.emit (node:events:527:28)","    at emit (node:internal/process/promises:140:20)","    at processPromiseRejections (node:internal/process/promises:274:27)","    at processTicksAndRejections (node:internal/process/task_queues:97:32)"]}Oct 19, 08:38:39 AM: f4e42876 [ERROR] [1666190320229] LAMBDA_RUNTIME Failed to get next invocation. Http Response code: 403Oct 19, 08:38:39 AM: f4e42876 2022-10-19T14:38:40.680Z	undefined	ERROR	Unhandled Promise Rejection 	{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"Error: Dynamic require of \"events\" is not supported","reason":{"errorType":"Error","errorMessage":"Dynamic require of \"events\" is not supported","stack":["Error: Dynamic require of \"events\" is not supported","    at file:///var/task/app/.netlify/functions-internal/render.mjs:29:9","    at node_modules/.pnpm/websocket@1.0.34/node_modules/websocket/lib/utils.js (file:///var/task/app/.netlify/functions-internal/render.mjs:5950:41)","    at __require2 (file:///var/task/app/.netlify/functions-internal/render.mjs:47:50)","    at node_modules/.pnpm/websocket@1.0.34/node_modules/websocket/lib/WebSocketServer.js (file:///var/task/app/.netlify/functions-internal/render.mjs:7457:18)","    at __require2 (file:///var/task/app/.netlify/functions-internal/render.mjs:47:50)","    at node_modules/.pnpm/websocket@1.0.34/node_modules/websocket/lib/websocket.js (file:///var/task/app/.netlify/functions-internal/render.mjs:8445:17)","    at __require2 (file:///var/task/app/.netlify/functions-internal/render.mjs:47:50)","    at node_modules/.pnpm/websocket@1.0.34/node_modules/websocket/index.js (file:///var/task/app/.netlify/functions-internal/render.mjs:8461:22)","    at __require2 (file:///var/task/app/.netlify/functions-internal/render.mjs:47:50)","    at node_modules/.pnpm/@supabase+realtime-js@1.7.4/node_modules/@supabase/realtime-js/dist/main/RealtimeClient.js (file:///var/task/app/.netlify/functions-internal/render.mjs:8900:23)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: Dynamic require of \"events\" is not supported","    at process.<anonymous> (file:///var/runtime/index.mjs:1131:17)","    at process.emit (node:events:527:28)","    at emit (node:internal/process/promises:140:20)","    at processPromiseRejections (node:internal/process/promises:274:27)","    at processTicksAndRejections (node:internal/process/task_queues:97:32)"]}Oct 19, 08:38:39 AM: f4e42876 RequestId: f4e42876-9c1d-4083-8e5b-90e04f54075a Error: Runtime exited with error: exit status 128Oct 19, 08:38:39 AM: f4e42876 Runtime.ExitErrorOct 19, 08:38:40 AM: f4e42876 Duration: 477.73 ms	Memory Usage: 48 MB	

And my build settings, if that helps:

Base directory /app
Build command npm run build
Publish directory /app/build
Deploy log visibility Logs are public
Builds Active

Any suggestions? I’m going a little crazy… Thanks!

Best,
René

Found the problem, my /app directory had a netlify.toml that evidently misdirected Netlify. Removing it from the repo solved the error. Its contents were

[build]
  command = "npm run build"
  publish = "build"
 
[dev]
 command = "svelte-kit dev"
 
[functions]
  directory = "app/netlify/functions"
  node_bundler = "esbuild"

Think I had just copy-pasted it from another project. Anyway, all good now.

Thanks for coming back and taking the time to share! This will help future forums members who encounter something similar.

Happy building :rocket: