Deploy a Mastra Server
Mastra compiles your application into a standalone Node.js server that can run on any platform supporting Node.js, Bun, or Deno.
This guide covers deploying the standalone server generated by mastra build. If you need to integrate Mastra into an existing Express or Hono application, see Server Adapters instead.
Building your applicationDirect link to Building your application
Run the build command from your project root:
mastra build
This creates a .mastra directory containing your production-ready server.
Read the mastra build reference for all available flags.
Build outputDirect link to Build output
After building, Mastra creates the following structure:
.mastra/
├── .build/ # Intermediate build artifacts (module maps, analysis)
└── output/
├── index.mjs # Server entry point
├── mastra.mjs # Your bundled Mastra configuration
├── tools.mjs # Aggregated tool exports
├── tools/ # Individual tool bundles
├── package.json # Production dependencies
├── node_modules/ # Installed dependencies
├── .npmrc # Copied from your project (if present)
├── public/ # Static assets (if src/mastra/public exists)
└── playground/ # Studio UI (if --studio flag used)
The output directory is self-contained. You can copy it to any server and run it directly.
Running the serverDirect link to Running the server
Start the server using the Mastra CLI:
mastra start
Or run directly with Node.js:
node .mastra/output/index.mjs
The mastra start command provides additional features:
- Loads environment variables from
.env.productionand.env - Provides helpful error messages for missing modules
- Handles process signals for graceful shutdown
Read the mastra start reference for all available flags.
Build configurationDirect link to Build configuration
Public folderDirect link to Public folder
If a public folder exists in your Mastra directory (src/mastra/public), its contents are copied to the output directory during build. These files are served as static assets by the server.
Mastra configurationDirect link to Mastra configuration
The build process respects configuration in your Mastra instance. For server behavior like CORS, timeouts, and middleware, see server overview. For all available options, see the configuration reference.
Build processDirect link to Build process
The build follows these steps:
- Locates entry file: Finds
index.tsorindex.jsin your Mastra directory. - Discovers tools: Scans for tool files matching
{mastraDir}/tools/**/*.{js,ts}, excluding test files. - Analyzes dependencies: Determines which packages to bundle vs. install externally.
- Bundles code: Uses Rollup with tree-shaking and optional source maps.
- Generates server: Creates a Hono-based HTTP server as
index.mjs. - Installs dependencies: Runs
npm installin the output directory. - Copies assets: Copies
publicfolder and.npmrcif present.
Environment variablesDirect link to Environment variables
| Variable | Description |
|---|---|
PORT | Server port (default: 4111) |
MASTRA_STUDIO_PATH | Path to Studio build directory (default: ./playground) |
MASTRA_SKIP_DOTENV | Skip loading .env files when set |
NODE_OPTIONS | Node.js options (e.g., --max-old-space-size=4096 for build memory issues) |
Server endpointsDirect link to Server endpoints
The built server exposes endpoints for health checks, agents, workflows, and more:
| Endpoint | Description |
|---|---|
GET /health | Health check endpoint, returns 200 OK |
GET /openapi.json | OpenAPI specification (if server.build.openAPIDocs is enabled) |
GET /swagger-ui | Interactive API documentation (if server.build.swaggerUI is enabled) |
This list is not exhaustive. To view all endpoints, run mastra dev and visit http://localhost:4111/swagger-ui.
To add your own endpoints, see Custom API Routes.
TroubleshootingDirect link to Troubleshooting
Memory errors during buildDirect link to Memory errors during build
If you encounter JavaScript heap out of memory errors:
NODE_OPTIONS="--max-old-space-size=4096" mastra build
RelatedDirect link to Related
- Server Overview - Configure server behavior, middleware, and authentication
- Server Adapters - Use Express or Hono instead of
mastra build - Custom API Routes - Add custom HTTP endpoints
- Configuration Reference - Full configuration options