I have a sveltekit function that streams responses from OpenAI and has this configuration:
// Set the runtime to edge for best performance
export const config = {
runtime: 'edge',
}
When I deploy it to Vercel, I get this error:
@cozemble/frontend-ai-playground:build: .svelte-kit/output/server/chunks/nanoids.js:1:19:
[16:03:05.239] @cozemble/frontend-ai-playground:build: 1 │ import crypto from "crypto";
[16:03:05.239] @cozemble/frontend-ai-playground:build: ╵ ~~~~~~~~
[16:03:05.239] @cozemble/frontend-ai-playground:build:
[16:03:05.240] @cozemble/frontend-ai-playground:build: The package "crypto" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
[16:03:05.240] @cozemble/frontend-ai-playground:build:
[16:03:05.246] @cozemble/frontend-ai-playground:build: [31merror during build:
[16:03:05.246] @cozemble/frontend-ai-playground:build: Error: Build failed with 1 error:
[16:03:05.246] @cozemble/frontend-ai-playground:build: .svelte-kit/output/server/chunks/nanoids.js:1:19: ERROR: Could not resolve "crypto"
[16:03:05.246] @cozemble/frontend-ai-playground:build: at failureErrorWithLog (/vercel/path0/node_modules/.pnpm/esbuild@0.18.20/node_modules/esbuild/lib/main.js:1649:15)
[16:03:05.246] @cozemble/frontend-ai-playground:build: at /vercel/path0/node_modules/.pnpm/esbuild@0.18.20/node_modules/esbuild/lib/main.js:1058:25
[16:03:05.246] @cozemble/frontend-ai-playground:build: at /vercel/path0/node_modules/.pnpm/esbuild@0.18.20/node_modules/esbuild/lib/main.js:1003:52
[16:03:05.247] @cozemble/frontend-ai-playground:build: at buildResponseToResult (/vercel/path0/node_modules/.pnpm/esbuild@0.18.20/node_modules/esbuild/lib/main.js:1056:7)
[16:03:05.247] @cozemble/frontend-ai-playground:build: at /vercel/path0/node_modules/.pnpm/esbuild@0.18.20/node_modules/esbuild/lib/main.js:1085:16
[16:03:05.247] @cozemble/frontend-ai-playground:build: at responseCallbacks.<computed> (/vercel/path0/node_modules/.pnpm/esbuild@0.18.20/node_modules/esbuild/lib/main.js:703:9)
[16:03:05.247] @cozemble/frontend-ai-playground:build: at handleIncomingPacket (/vercel/path0/node_modules/.pnpm/esbuild@0.18.20/node_modules/esbuild/lib/main.js:762:9)
[16:03:05.247] @cozemble/frontend-ai-playground:build: at Socket.readFromStdout (/vercel/path0/node_modules/.pnpm/esbuild@0.18.20/node_modules/esbuild/lib/main.js:679:7)
[16:03:05.247] @cozemble/frontend-ai-playground:build: at Socket.emit (node:events:514:28)
[16:03:05.247] @cozemble/frontend-ai-playground:build: at addChunk (node:internal/streams/readable:324:12)[39m
[16:03:05.282] @cozemble/frontend-ai-playground:build: ELIFECYCLE Command failed with exit code 1.
[16:03:05.296] @cozemble/frontend-ai-playground:build: ERROR: command finished with error: command (/vercel/path0/frontend/ai-playground) pnpm run build exited (1)
[16:03:05.296] command (/vercel/path0/frontend/ai-playground) pnpm run build exited (1)
If I drop the edge runtime, so it uses node (I assume) then I can deploy, but my functions seem to time out after 60 seconds. So I need the edge functions.
I am using "openai": "4.6.0"
and "ai": "^2.2.12",
This is my svelte.config.js
import adapter from '@sveltejs/adapter-auto'
import { vitePreprocess } from '@sveltejs/kit/vite'
/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
// for more information about preprocessors
preprocess: vitePreprocess(),
kit: {
adapter: adapter(),
},
}
export default config
How can I make this work?
vercel/ai
is actually fine, and has been fixed for this issue (package "crypto" not found) with this pull request - https://github.com/vercel/ai/pull/300
The actual issue is that my own code was also importing nanoid
. If you are interested in how, see this commit - https://github.com/cozemble/monorepo/commit/35ec43305b37e56ab3f593f9b30901d2d1ab7c6a
So when I also made my use of nanoid
not require crypto
, my edge function deployed fine.