SvelteKit部署Vercel Edge Runtime遇crypto模块解析错误的解决方法
解决SvelteKit Edge Runtime部署Vercel时的crypto模块解析错误
问题背景
有一个流式返回OpenAI响应的SvelteKit函数,配置Edge Runtime以避免超时:
// Set the runtime to edge for best performance export const config = { runtime: 'edge', }
部署到Vercel时出现以下构建错误:
@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)
移除Edge Runtime改用Node Runtime可正常部署,但函数会在60秒后超时,因此必须使用Edge Runtime。当前依赖版本:"openai": "4.6.0"、"ai": "^2.2.12",svelte.config.js配置如下:
import adapter from '@sveltejs/adapter-auto' import { vitePreprocess } from '@sveltejs/kit/vite' /** @type {import('@sveltejs/kit').Config} */ const config = { preprocess: vitePreprocess(), kit: { adapter: adapter(), }, } export default config
解决方案
方案1:升级依赖版本
旧版本的ai和openai库可能依赖Node.js的crypto模块,而Edge Runtime不支持该模块。升级到最新兼容版本,通常能解决模块依赖问题:
pnpm update ai openai
方案2:配置Vite处理crypto模块
修改svelte.config.js,添加Vite配置,让构建工具适配Edge Runtime的Web Crypto API:
import adapter from '@sveltejs/adapter-auto' import { vitePreprocess } from '@sveltejs/kit/vite' /** @type {import('@sveltejs/kit').Config} */ const config = { preprocess: vitePreprocess(), kit: { adapter: adapter(), vite: { optimizeDeps: { esbuildOptions: { // 将全局crypto指向Edge Runtime的Web Crypto define: { 'globalThis.crypto': 'crypto' } } }, resolve: { // 用browser版本的crypto polyfill替换Node模块 alias: { crypto: 'crypto-browserify' } } } }, } export default config
同时安装crypto-browserify作为开发依赖:
pnpm add -D crypto-browserify
方案3:强制使用nanoid的Web兼容版本
错误源于nanoid模块引用了Node的crypto,新版本nanoid默认使用Web Crypto。在package.json中添加依赖覆盖:
{ "pnpm": { "overrides": { "nanoid": "^4.0.0" } } }
验证
修改完成后,执行pnpm run build本地验证构建是否成功,再重新部署到Vercel,确认流式响应正常且无超时问题。
内容的提问来源于stack exchange,提问作者Mike Hogan
相关产品推荐
相关产品推荐

