The Wild

Cloudflare Workers API Migration Node.js Compatibility Checklist

· 7 min read · Mammals

In this article
  1. Background: Why Node.js APIs Need a Migration Checklist
  2. Runtime API Compatibility: What Works and What Doesn’t
  3. Module System and Bundling: ESM vs. CommonJS
  4. Storage and State: Replacing fs and Databases
  5. Common Problems and How to Solve Them
  6. Comparison: Workers vs. Node.js on a VPS or Container
  7. Migration Steps: A Practical Checklist
  8. Costs and Limits to Keep in Mind
  9. Safety and Security Considerations
  10. Frequently Asked Questions

Yes, you can migrate an existing Node.js API to Cloudflare Workers, but you must adapt it to the Workers runtime, which is not a drop-in Node.js environment. This checklist covers the key compatibility areas—runtime APIs, modules, storage, and deployment—so you know exactly what to change before you start.

Background: Why Node.js APIs Need a Migration Checklist

Cloudflare Workers runs on the V8 JavaScript engine, not Node.js. It provides a set of Web Standard APIs (like fetch, Request, Response, ReadableStream) instead of Node’s built-in modules (http, fs, crypto, stream). Many popular npm packages rely on Node-specific features, so they won’t work without modification or a compatibility layer. Understanding these differences early prevents wasted effort and runtime surprises.

Runtime API Compatibility: What Works and What Doesn’t

Start by auditing every API your Node.js code uses. The following are the most common gaps:

  • HTTP server: Replace http.createServer() with a Worker fetch event handler. The Worker receives a Request and returns a Response.
  • File system: There is no fs module. Use Cloudflare’s KV (key-value store), R2 (object storage), or D1 (SQLite) for persistence.
  • Streams: Node’s stream module is not available, but Web Streams (ReadableStream, WritableStream) are supported. Many libraries like busboy for multipart parsing have Worker-compatible alternatives.
  • Timers: setTimeout and setInterval work, but be aware of Worker execution limits (CPU time, not wall-clock time). Long-running timers may be terminated.
  • Global objects: Buffer is not available by default, but you can use Uint8Array or add a polyfill. process is not available; use environment variables via env bindings.

For a complete list, refer to the official Cloudflare Workers documentation on “Supported APIs” and “Differences between Workers and Node.js.”

Module System and Bundling: ESM vs. CommonJS

Workers support ES Modules (ESM) and CommonJS, but the recommended approach is ESM. If your Node.js project uses require(), you can still migrate, but you’ll need a bundler like esbuild or webpack to resolve dependencies. Cloudflare’s Wrangler CLI automatically bundles your code and dependencies, but it cannot polyfill Node’s built-in modules unless you provide a compatibility flag or a custom shim.

For npm packages that rely on Node globals (like process or Buffer), you can use the nodejs_compat compatibility flag in your wrangler.toml. This enables a subset of Node APIs, but it is not a full Node environment. Test each dependency carefully.

Storage and State: Replacing fs and Databases

Node.js APIs often read configuration files, write logs, or use SQLite/PostgreSQL. In Workers, you have these options:

  • Environment variables: For static config, use [vars] in wrangler.toml or secrets via the dashboard.
  • KV: For simple key-value data, like user sessions or feature flags. Reads are fast, but writes are eventually consistent.
  • D1: A serverless SQLite database with a familiar SQL interface. Good for relational data.
  • R2: Object storage for files, images, or large blobs. No local file system.
  • Durable Objects: For coordinated state and WebSockets, if you need single-point consistency.

Migrate your database queries to D1’s SQL dialect or use an HTTP-based database like Turso or Supabase via fetch.

Common Problems and How to Solve Them

Even with careful planning, you’ll hit issues. Here are the most frequent:

  1. “Module not found” for Node built-ins: Add the nodejs_compat flag or replace the module with a Web API equivalent.
  2. Memory limits: Workers have a 128 MB memory limit (as of this writing). Large buffers or streams can exceed it. Use streaming and avoid loading entire files into memory.
  3. Execution time limits: Free tier allows 10 ms CPU time; paid plans allow 30 seconds CPU per invocation. Optimize heavy computations or offload to a service.
  4. Cold starts: Workers are generally fast, but if your code loads large dependencies, cold starts increase. Keep your bundle small.
  5. Environment variables: Unlike Node’s process.env, you access them via the env parameter in the fetch handler. Update all references.

Comparison: Workers vs. Node.js on a VPS or Container

Workers are not a direct replacement for a Node.js server. They are serverless functions that run on the edge. The main differences:

  • Scaling: Workers scale automatically to zero and handle millions of requests, but you don’t control the underlying server.
  • Pricing: Workers have a free tier and usage-based pricing. A VPS has a fixed monthly cost.
  • Latency: Workers run in 300+ locations worldwide, so users get responses from a nearby server. A single VPS may be farther away.
  • Long-lived connections: WebSockets are supported, but Durable Objects are needed for stateful connections. A Node.js server can maintain persistent connections more easily.
  • Dependencies: Node.js can use any npm package that works with its runtime. Workers require packages that are compatible with the Workers runtime or that can be polyfilled.

Choose Workers if you need global edge deployment and don’t need a persistent server. Choose Node.js on a VPS if you need full control over the runtime, long-running processes, or specific Node-only modules.

Migration Steps: A Practical Checklist

Follow these steps to migrate your API systematically:

  1. Audit your dependencies: List every npm package and check if it uses Node APIs. Use the workers-sdk or compat tools to identify issues.
  2. Set up a new Workers project: Run npx wrangler init my-api and choose the “Fetch handler” template.
  3. Convert your request handling: Replace your Express/Fastify routes with a single fetch handler that routes based on request.url and request.method.
  4. Replace Node-specific code: Swap fs with KV/R2, http with fetch, and Buffer with Uint8Array or a polyfill.
  5. Configure bindings: Add KV, D1, or R2 namespaces to your wrangler.toml and pass them via the env parameter.
  6. Test locally: Use wrangler dev to run your Worker locally. Write tests for your endpoints.
  7. Deploy: Run wrangler deploy. Monitor logs via the Cloudflare dashboard.

Costs and Limits to Keep in Mind

Cloudflare Workers has a free tier and paid plans. As of this writing, the free tier includes 100,000 requests per day and 10 ms CPU time per request. Paid plans (starting at $5 per month) include 10 million requests per month and 30 seconds CPU time per request. Storage (KV, D1, R2) has separate pricing. Always check the current pricing on Cloudflare’s website, as these numbers can change.

Memory is limited to 128 MB per Worker, and there is a limit on the size of your Worker script (1 MB after compression). If your API needs more, consider splitting it into multiple Workers or using a different platform.

Safety and Security Considerations

When migrating, pay attention to security:

  • Secrets: Store API keys and tokens as secrets in Cloudflare, not in code or environment variables that are visible in the dashboard.
  • Input validation: Workers run on the edge, so validate all input to prevent injection attacks, just as you would in Node.js.
  • Dependencies: Only use packages that are actively maintained and compatible with Workers. Vulnerable Node packages may not be patched for the Workers runtime.
  • Rate limiting: Cloudflare provides rate limiting features, but you can also implement your own in the Worker using KV or Durable Objects.

Frequently Asked Questions

Can I use Express or Fastify with Cloudflare Workers?

Not directly. Express and Fastify rely on Node’s HTTP module. You can use community adapters like @cloudflare/workers-express or hono, which is a framework designed for Workers. Alternatively, rewrite your routes using plain fetch handlers.

Does the nodejs_compat flag make all Node.js packages work?

No. It enables a subset of Node’s core modules (like crypto, stream, util) but not everything. Packages that use fs, http, or child_process still won’t work. Test each package individually.

How do I handle file uploads in a Worker?

Workers support Request.formData() for multipart uploads. You can stream the file to R2 or process it in memory if it’s small. For large files, use R2’s multipart upload API.

What is the difference between Workers and Cloudflare Pages Functions?

Pages Functions are built on the same Workers runtime but are designed for full-stack applications on Pages. They use file-based routing and are ideal for server-side rendering. Workers are more flexible for standalone APIs.

Can I migrate a WebSocket server to Workers?

Yes, Workers support WebSockets, but for stateful connections you need Durable Objects. A simple WebSocket proxy works without Durable Objects, but maintaining per-client state requires them.

Will my API be slower after migrating to Workers?

Not necessarily. Workers run on the edge, so latency can be lower for global users. However, if your API relies on a single database far from the edge, you may see higher latency. Use D1 or a global database to minimize this.