关于 workers-migration
面向将现有应用迁移到 Cloudflare Workers 的开发者,涵盖 AWS Lambda、Vercel、Express 等平台的迁移模式。还包括 Node.js API 兼容性说明、迁移清单和常见错误排查。
主要功能
- 01平台迁移决策指引
- 02Lambda 与 Express 迁移示例
- 03Node.js API 兼容性说明
- 04迁移检查清单
- 05常见迁移错误排查
适用场景
- 01将 Lambda 函数迁移到 Workers
- 02迁移 Vercel 或 Next.js 应用
- 03调整 Express 或 Node.js 应用
- 04排查不受支持的 Node.js API
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ secondsky/ claude-skills --skill workers-migration安装后在 Claude Code 中输入/workers-migration确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/express-migration.mdlambda-migration.mdnode-compatibility.mdvercel-migration.mdscripts/analyze-migration.shtemplates/express-adapter.tslambda-adapter.tsnode-compat.tsvercel-adapter.ts详细介绍
Workers 迁移指南
将现有应用从各种平台迁移到 Cloudflare Workers。
迁移决策树
What are you migrating from?
├── AWS Lambda
│ └── Node.js handler? → Lambda adapter pattern
│ └── Python? → Consider Python Workers
│ └── Container/custom runtime? → May need rewrite
├── Vercel/Next.js
│ └── API routes? → Minimal changes with adapter
│ └── Full Next.js app? → Use OpenNext adapter
│ └── Middleware? → Direct Workers equivalent
├── Express/Node.js
│ └── Simple API? → Hono (similar API)
│ └── Complex middleware? → Gradual migration
│ └── Heavy node: usage? → Compatibility layer
└── Other Edge (Deno Deploy, Fastly)
└── Standard Web APIs? → Minimal changes
└── Platform-specific? → Targeted rewrites
平台对比
| 特性 | Workers | Lambda | Vercel | Express |
|---|---|---|---|---|
| 冷启动 | ~0ms | 100-500ms | 10-100ms | 不适用 |
| CPU 限制 | 50ms/10ms | 15 min | 10s | 无 |
| 内存 | 128MB | 10GB | 1GB | 系统 |
| 最大响应 | 6MB(流式响应不限) | 6MB | 4.5MB | 无 |
| 全球边缘节点 | 300+ PoPs | 区域性 | ~20 PoPs | 手动 |
| Node.js API | 部分支持 | 完全支持 | 完全支持 | 完全支持 |
迁移中最常见的 10 个错误
| 错误 | 来源 | 原因 | 解决方案 |
|---|---|---|---|
fs is not defined | Lambda/Express | 文件系统访问 | 使用 KV/R2 存储 |
Buffer is not defined | Node.js | Node.js 全局变量 | 从 node:buffer 导入 |
process.env undefined | 全部 | 环境变量访问方式 | 使用 env 参数 |
setTimeout not returning | Lambda | 异步模式 | 使用 ctx.waitUntil() |
require() not found | Express | CommonJS | 转换为 ESM 导入 |
Exceeded CPU time | 全部 | 长时间计算 | 分块处理或使用 DO |
body already consumed | Express | 请求正文 | 读取前先克隆 |
Headers not iterable | Lambda | Headers API | 使用 Headers 构造函数 |
crypto.randomBytes | Node.js | Node crypto | 使用 crypto.getRandomValues |
Cannot find module | 全部 | 缺少 polyfill | 检查 Workers 兼容性 |
快速迁移模式
AWS Lambda 处理程序
// Before: AWS Lambda
export const handler = async (event, context) => {
const body = JSON.parse(event.body);
return {
statusCode: 200,
body: JSON.stringify({ message: 'Hello' }),
};
};
// After: Cloudflare Workers
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const body = await request.json();
return Response.json({ message: 'Hello' });
},
};
Express 中间件
// Before: Express
app.use((req, res, next) => {
if (!req.headers.authorization) {
return res.status(401).json({ error: 'Unauthorized' });
}
next();
});
// After: Hono Middleware
app.use('*', async (c, next) => {
if (!c.req.header('Authorization')) {
return c.json({ error: 'Unauthorized' }, 401);
}
await next();
});
环境变量
// Before: Node.js
const apiKey = process.env.API_KEY;
// After: Workers
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const apiKey = env.API_KEY;
// ...
},
};
Node.js 兼容性
Workers 通过兼容性标志支持许多 Node.js API:
// wrangler.jsonc
{
"compatibility_flags": ["nodejs_compat_v2"],
"compatibility_date": "2024-12-01"
}
通过 nodejs_compat_v2 支持:
crypto(大多数方法)buffer(Buffer 类)util(promisify、types)stream(Readable、Writable)events(EventEmitter)path(所有方法)string_decoderassert
不支持(需要替代方案):
fs→ 使用 R2/KVchild_process→ 无法实现cluster→ 不适用dgram→ 不支持net→ 使用 fetch/WebSockettls→ 由平台处理
何时加载参考资料
| 参考资料 | 加载时机 |
|---|---|
references/lambda-migration.md | 迁移 AWS Lambda 函数时 |
references/vercel-migration.md | 从 Vercel/Next.js 迁移时 |
references/express-migration.md | 迁移 Express/Node.js 应用时 |
references/node-compatibility.md | 遇到 Node.js API 兼容性问题时 |
迁移检查清单
- 分析依赖项:检查是否使用不受支持的 Node.js API
- 转换为 ESM:将 require() 替换为 import
- 更新环境变量访问方式:使用 env 参数,而不是 process.env
- 替换文件系统:使用 R2/KV 存储
- 处理异步任务:使用 ctx.waitUntil() 执行后台任务
- 本地测试:使用 wrangler dev 验证
- 性能测试:确保不超过 CPU 限制
另请参阅
workers-runtime-apis- Workers 中可用的 APIworkers-performance- 优化技巧cloudflare-worker-base- Workers 基本设置
评论
支持 Markdown