Prerequisites:
- A Netlify account
- Node.js version 22 or higher
- A Mastra project ready to deploy
Installation
Configure the deployer
Add the Netlify deployer to your Mastra configuration:
The Netlify deployer requires no configuration options. It automatically sets up everything needed for Netlify Functions.
Build your project
Run your build command. The deployer will:This generates:
- Bundle your application for Netlify Functions
- Install dependencies with Linux x64 architecture
- Generate Netlify Frameworks API configuration
- Set up routing to the API function
.netlify/v1/functions/api/- Your bundled function and dependencies.netlify/v1/config.json- Netlify Frameworks API configuration
How it works
The Netlify deployer:- Bundles your application using Rollup with ESM shim enabled
- Creates an entry point using Hono’s Netlify adapter:
-
Installs dependencies with specific architecture targeting:
- OS:
linux - CPU:
x64 - libc:
gnu
- OS:
-
Generates Frameworks API config (
.netlify/v1/config.json):
- Points Netlify to the functions directory
- Disables Netlify’s bundling (Mastra pre-bundles)
- Routes all requests to your API function
Storage compatibility
Use these storage options instead:@mastra/pg- PostgreSQL (recommended)@mastra/mongodb- MongoDB@mastra/dynamodb- AWS DynamoDB
Environment variables
Environment variables can be configured in multiple ways:- Local
.envfiles - Automatically loaded during development - Netlify Dashboard - Set in Site Settings → Environment Variables
- Netlify CLI - Use
netlify envcommands
netlify.toml- For build-time variables:
Project structure
After building, your project will have this structure:Custom Netlify configuration
You can add anetlify.toml file for additional configuration:
The deployer’s automatic configuration takes precedence for function bundling and routing, but you can use
netlify.toml for other settings like build commands and environment variables.Deployment options
Using Netlify CLI
Using Git integration
- Push your code to GitHub, GitLab, or Bitbucket
- Connect your repository in the Netlify dashboard
- Configure build settings:
- Build command:
pnpm build - Publish directory: Leave empty (functions are auto-detected)
- Build command:
- Deploy automatically on push
Using Netlify Drop
For manual deployments:- Build your project locally:
pnpm build - Drag and drop the
.netlifyfolder to Netlify Drop
Troubleshooting
Build fails with 'Cannot find module' error
Build fails with 'Cannot find module' error
The deployer bundles your code and installs dependencies separately. Make sure:
- All dependencies are in
package.json - You’re using the correct import paths
- ESM syntax is used consistently
Function not found / 404 errors
Function not found / 404 errors
Verify that:
- The
.netlify/v1/config.jsonfile was generated - Your build completed successfully
- The function is in
.netlify/v1/functions/api/
Error: @libsql/client not supported
Error: @libsql/client not supported
The deployer automatically detects
@mastra/libsql and throws an error because native bindings don’t work in Netlify Functions. Switch to @mastra/pg or another compatible storage option.Architecture mismatch errors
Architecture mismatch errors
The deployer automatically installs dependencies for Linux x64. If you see errors related to native modules:
- Clear
node_modulesand rebuild - Check that the package supports Linux x64
- Consider using a different package
Function timeout
Function timeout
Netlify Functions have a default timeout of 10 seconds (26 seconds for Pro plans). For longer operations:
- Upgrade to a Pro plan
- Use background functions for async tasks
- Optimize your code for faster execution
Performance tips
- Enable caching - Use Netlify’s cache headers for static responses
- Minimize dependencies - Keep your bundle size small
- Use background functions - For long-running tasks
- Configure regions - Deploy closer to your users (available on Pro plans)
Next steps
Server & API
Learn about Mastra’s server capabilities
Storage options
Explore compatible storage backends
Observability
Set up logging and tracing
Netlify Docs
Official Netlify Functions documentation