What Causes This Error?
This error occurs when Astro can’t parse or load your actions file. This typically happens due to syntax errors, invalid imports, or incorrect file structure.
The Problem
// src/actions/index.ts
import { defineAction } from 'astro:actions';
// ❌ Syntax error or invalid code
export const server = {
subscribe: defineAction({
handler: async () => {
return { success: true } // Missing comma
}
}) // Missing closing brace
The Fix
Check for Syntax Errors
// src/actions/index.ts
import { defineAction, z } from 'astro:actions';
// ✅ Valid syntax
export const server = {
subscribe: defineAction({
input: z.object({
email: z.string().email(),
}),
handler: async ({ email }) => {
return { success: true };
},
}),
};
Common Scenarios
Missing Imports
// ❌ Missing imports
export const server = {
subscribe: defineAction({ // defineAction is not defined
input: z.object({ // z is not defined
email: z.string(),
}),
handler: async () => {},
}),
};
// ✅ Include all imports
import { defineAction, z } from 'astro:actions';
export const server = {
subscribe: defineAction({
input: z.object({
email: z.string(),
}),
handler: async ({ email }) => {
return { success: true };
},
}),
};
Wrong File Location
# ❌ Wrong locations
src/actions.ts # Should be in folder
actions/index.ts # Should be in src/
src/action/index.ts # Folder name should be 'actions'
# ✅ Correct location
src/actions/index.ts
Invalid Export
// ❌ Wrong export patterns
module.exports = {
subscribe: defineAction({ /* ... */ }),
};
export default {
subscribe: defineAction({ /* ... */ }),
};
export const actions = {
subscribe: defineAction({ /* ... */ }),
};
// ✅ Must use named 'server' export
export const server = {
subscribe: defineAction({
handler: async () => {
return { success: true };
},
}),
};
Invalid Handler
// ❌ Handler must be async function
export const server = {
subscribe: defineAction({
handler: () => { // Missing async
return { success: true };
},
}),
};
// ❌ Arrow function with wrong syntax
export const server = {
subscribe: defineAction({
handler: async input => ({ success: true }), // May cause issues
}),
};
// ✅ Proper async handler
export const server = {
subscribe: defineAction({
handler: async (input) => {
return { success: true };
},
}),
};
Circular Dependencies
// ❌ Importing from files that import actions
import { helper } from './helpers'; // If helpers imports actions
// ✅ Keep actions self-contained or use careful imports
import { db } from '../lib/db'; // OK if db doesn't import actions
Invalid Input Schema
// ❌ Invalid Zod schema
export const server = {
subscribe: defineAction({
input: { // Must be Zod schema
email: 'string',
},
handler: async () => {},
}),
};
// ✅ Use Zod properly
import { z } from 'astro:actions';
export const server = {
subscribe: defineAction({
input: z.object({
email: z.string().email(),
}),
handler: async ({ email }) => {
return { success: true };
},
}),
};
TypeScript Errors
// ❌ TypeScript errors prevent loading
export const server = {
subscribe: defineAction({
handler: async (input: WrongType) => { // WrongType doesn't exist
return { success: true };
},
}),
};
// ✅ Fix TypeScript errors
export const server = {
subscribe: defineAction({
input: z.object({
email: z.string(),
}),
handler: async ({ email }) => { // Type inferred from schema
return { success: true };
},
}),
};
Runtime Errors in Module
// ❌ Top-level code that throws
const config = require('./missing-file.json'); // File doesn't exist
// ❌ Immediate function that fails
const data = JSON.parse(invalidJson);
// ✅ Move risky code inside handlers
export const server = {
loadConfig: defineAction({
handler: async () => {
// Load config inside handler, not at module level
const config = await import('./config.json');
return config;
},
}),
};
Debug the Issue
# Check for syntax errors
npx tsc --noEmit src/actions/index.ts
# Check console for detailed error
npm run dev
# Look for error details in terminal output
Minimal Working Example
// Start with minimal actions file
import { defineAction, z } from 'astro:actions';
export const server = {
test: defineAction({
handler: async () => {
return { message: 'It works!' };
},
}),
};
Quick Checklist
- Check for JavaScript/TypeScript syntax errors
- Verify file is at
src/actions/index.ts - Import
defineActionandzfromastro:actions - Export as
server, not default - Use proper Zod schemas for input
- Ensure handlers are async functions
- Check for circular dependencies