We use cookies to ensure you get the best experience on our website. Read our privacy policy

Skip to main content
Astro server error with message bubbles
Home Blog Debug

Expected `server` export in actions file to be an object. Received undefined

Troubleshoot and resolve the common "server export in actions file is undefined" error in AstroJS server-side actions with this practical guide.

Published on:
Check Circle Difficulty: easy

When working with AstroJS server-side actions, you might encounter this error:

Terminal window
Expected `server` export in actions file to be an object. Received undefined

This error occurs when Astro tries to use a server action but can’t find the expected server export object in your actions file.

This error typically occurs when you try to import client-side code into a server actions file. The most common scenario is importing code that uses browser-specific types or APIs (like HTMLElement, window, or document) in a file that needs to run on the server.

Limited Time Launch Sale

Save time debugging with our Astro starter template that includes properly configured server actions!

GET 60% OFF!
src/actions/index.ts
import { validateForm } from './clientUtils'; // This file uses HTMLElement
export const server = {
submitForm: async (data) => {
// This will fail because validateForm uses browser-only types
const isValid = validateForm(data);
// ...
},
};
  1. Separate your client and server code
  2. Keep browser-specific code in client-side files only
  3. Implement server-side validation separately

Here’s the corrected approach:

src/actions/index.ts
export const server = {
submitForm: async (data) => {
// Implement server-side validation here
const isValid = validateServerSide(data);
// ...
},
};
function validateServerSide(data) {
// Server-safe validation logic
return true;
}

The best way to prevent this error is to create a protection.ts file that throws an error if the file is imported by a file that is not server side. Import this file in all your server only files.

src/lib/server/protection.ts
if (typeof window !== 'undefined') {
throw new Error('This file is not allowed to be imported by a client-side file');
}
  • Check Circle

    Always check if your imported utilities use browser-specific APIs

  • Check Circle

    Keep client-side validation separate from server-side validation

  • Check Circle

    Use TypeScript to catch these issues early in development

Related Articles

Read more Debug articles
North Star Themes Logo

Subscribe to our newsletter

Get the latest AstroJS tips and tricks right to your inbox

Email: [email protected]

© 2025 North Star Themes

Web Kit Provided By North Star Themes