Fix Error: Error: Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server". in Next.js
This error occurs when a server function is passed as a prop to a client component without the 'use server' directive. In the App Router, functions crossing the server-client boundary must be explicitly marked as server actions. Fix it by adding 'use server' to the function or the file containing it.
Reading the Stack Trace
Here's what each line means:
- at ServerPage (/app/src/app/dashboard/page.tsx:14:5): The server component at line 14 passes a function prop to a client component without marking it as a server action.
- at renderModelDestructive (/app/node_modules/next/dist/compiled/react-server-dom-webpack/cjs/react-server-dom-webpack-server.edge.development.js:924:14): React Server Components serializer encounters a function and cannot serialize it across the server-client boundary.
- at renderElement (/app/node_modules/next/dist/compiled/react-server-dom-webpack/cjs/react-server-dom-webpack-server.edge.development.js:798:12): The RSC renderer is processing the element tree when it detects the non-serializable function prop.
Common Causes
1. Passing an inline function to a client component
A server component defines an inline async function and passes it as a prop to a client component without 'use server'.
// app/dashboard/page.tsx (Server Component)
import DeleteButton from './DeleteButton';
export default async function Page() {
async function deleteItem(id: string) {
await db.item.delete({ where: { id } });
}
return <DeleteButton onDelete={deleteItem} />;
}
2. Importing server function in client component
A client component imports and uses a function from a module that does not have the 'use server' directive.
// lib/actions.ts (no 'use server' directive)
export async function deleteItem(id: string) {
await db.item.delete({ where: { id } });
}
// components/DeleteButton.tsx
'use client';
import { deleteItem } from '@/lib/actions';
export function DeleteButton() {
return <button onClick={() => deleteItem('1')}>Delete</button>;
}
3. Passing a callback to a form action
A server component passes a function to a form's action prop without marking it as a server action.
export default async function Page() {
async function handleSubmit(formData: FormData) {
await db.post.create({ data: { title: formData.get('title') } });
}
return <form action={handleSubmit}><input name="title" /><button>Submit</button></form>;
}
The Fix
Move the server function to a separate file with the 'use server' directive at the top. This tells Next.js the function is a server action that can be serialized as a reference and safely passed to client components.
// app/dashboard/page.tsx (Server Component)
import DeleteButton from './DeleteButton';
export default async function Page() {
async function deleteItem(id: string) {
await db.item.delete({ where: { id } });
}
return <DeleteButton onDelete={deleteItem} />;
}
// app/dashboard/page.tsx (Server Component)
import DeleteButton from './DeleteButton';
import { deleteItem } from '@/app/actions';
export default async function Page() {
return <DeleteButton onDelete={deleteItem} />;
}
// app/actions.ts
'use server';
export async function deleteItem(id: string) {
await db.item.delete({ where: { id } });
}
Testing the Fix
import { render, screen, fireEvent } from '@testing-library/react';
import DeleteButton from '@/app/dashboard/DeleteButton';
describe('DeleteButton', () => {
it('calls onDelete when clicked', async () => {
const onDelete = jest.fn();
render(<DeleteButton onDelete={onDelete} />);
fireEvent.click(screen.getByRole('button', { name: /delete/i }));
expect(onDelete).toHaveBeenCalled();
});
it('renders the delete button', () => {
render(<DeleteButton onDelete={jest.fn()} />);
expect(screen.getByRole('button', { name: /delete/i })).toBeInTheDocument();
});
});
Run your tests:
npm test
Pushing Through CI/CD
git checkout -b fix/server-action-use-server-directive,git add src/app/actions.ts src/app/dashboard/page.tsx src/app/dashboard/__tests__/DeleteButton.test.tsx,git commit -m "fix: add 'use server' directive to server action functions",git push origin fix/server-action-use-server-directive
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: npm test -- --coverage
- run: npm run build
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
npm install bugstack-sdk
Step 2: Initialize
import { initBugStack } from 'bugstack-sdk'
initBugStack({ apiKey: process.env.BUGSTACK_API_KEY })
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Create a separate actions file with the 'use server' directive.
- Run the test suite locally to verify server actions work.
- Open a pull request with the refactored server actions.
- Wait for CI checks to pass on the PR.
- Merge to main and verify the actions execute correctly in staging.
Frequently Asked Questions
BugStack validates server actions across the server-client boundary, confirms they serialize correctly, runs your test suite, and verifies the build before marking it safe.
Every fix is delivered as a pull request with full CI validation. Your team reviews and approves before anything reaches production.
Yes. Place 'use server' at the top of the file, and all exported async functions become server actions. You can also use 'use server' inside individual functions.
Server actions are POST endpoints behind the scenes. Always validate inputs and check authorization inside each action, just as you would with an API route.