Prerequisites:
- A Cloudflare account with Workers enabled
- Node.js version 22 or higher
- A Mastra project ready to deploy
wranglerCLI installed (optional peer dependency)
Installation
Install the Cloudflare deployer
Install the
@mastra/deployer-cloudflare package and wrangler:wrangler is an optional peer dependency, but recommended for local development and deployment.Build your project
Run your build command. The deployer will:
- Bundle your application for Cloudflare Workers
- Generate
wrangler.jsoncconfiguration - Create a TypeScript stub to reduce bundle size
Configuration options
TheCloudflareDeployer constructor accepts all standard wrangler.json configuration options. See the Wrangler configuration documentation for complete details.
Common options
name
- Type:
string - Required: Yes
- Description: Name of your Worker
- Example:
name: 'my-ai-app'
compatibility_date
- Type:
string - Description: Cloudflare Workers compatibility date
- Default:
'2025-04-01' - Example:
compatibility_date: '2025-04-01'
compatibility_flags
- Type:
string[] - Description: Enable runtime compatibility features
- Default:
['nodejs_compat', 'nodejs_compat_populate_process_env'] - Example:
compatibility_flags: ['nodejs_compat']
vars
- Type:
Record<string, string> - Description: Environment variables (merged with
.envfiles) - Example:
d1_databases
- Type:
Array<{ binding: string; database_name: string; database_id: string }> - Description: Cloudflare D1 database bindings
- Example:
kv_namespaces
- Type:
Array<{ binding: string; id: string }> - Description: Cloudflare KV namespace bindings
- Example:
routes
- Type:
Array<{ pattern: string; zone_name?: string; custom_domain?: boolean }> - Description: Custom domain routing
- Example:
How it works
The Cloudflare deployer:- Wraps your Mastra instance to work with Cloudflare’s request/response model:
- Creates a TypeScript stub to prevent bundling the full TypeScript library (~10MB)
- Generates
wrangler.jsoncwith your configuration and proper paths - Polyfills Node.js APIs for compatibility
- Handles PostgreSQL stores if using
@mastra/pg
Storage compatibility
Compatible storage options:@mastra/cloudflare-d1- Cloudflare D1 (recommended)@mastra/pg- PostgreSQL (with connection pooling)@mastra/mongodb- MongoDB@mastra/dynamodb- AWS DynamoDB
Environment variables
Environment variables are handled through:- Local
.envfiles - Automatically loaded and merged varsin configuration - Passed to the deployer constructor- Wrangler secrets - For sensitive data:
- Cloudflare Dashboard - Via the Workers settings UI
Variables from
.env files and the vars option are merged together, with vars taking precedence.Using Cloudflare bindings
D1 Database
KV Namespace
Project structure
After building, your project will have:Troubleshooting
Error: TypeScript library is too large
Error: TypeScript library is too large
This is handled automatically. The deployer creates a TypeScript stub to keep bundle sizes small while maintaining compatibility.
Module not found errors
Module not found errors
Ensure all imports use the correct module specifiers. Cloudflare Workers require explicit file extensions for ESM imports.
Error: @libsql/client not supported
Error: @libsql/client not supported
The deployer automatically detects
@mastra/libsql and throws an error. Switch to @mastra/cloudflare-d1 for Cloudflare Workers.Runtime errors with process.env
Runtime errors with process.env
Make sure you have the
nodejs_compat_populate_process_env compatibility flag enabled (it’s enabled by default).Next steps
Cloudflare D1 Storage
Use Cloudflare D1 for storage
Server & API
Learn about Mastra’s server capabilities
Observability
Set up logging and tracing
Wrangler Docs
Official Wrangler documentation