Server-side function modules
Define mutations or other server-only operations in a reusable module.
Next.js 16+ · Directive
use server marks a function or module for server execution. Use it at file scope for all exports or inside an asynchronous function for a single Server Function.
use server is a React directive supported by Next.js that keeps covered functions on the server. Put it at the top of a module to mark all exported functions, or at the top of an asynchronous function to mark only that function. Server Functions can be imported into Client Components from a dedicated server module and invoked through supported interactions.
'use server'// app/actions.ts
'use server'
export async function createUser(formData: FormData) {
const name = formData.get('name')
if (typeof name !== 'string' || !name.trim()) {
throw new Error('Name is required')
}
// Persist the validated value on the server.
return {name}
}
// app/page.tsx
import { createUser } from './actions'
export default function Page() {
return (
<form action={createUser}>
<input name="name" />
<button type="submit">Create user</button>
</form>
)
}A file-level directive marks the exported asynchronous function as a Server Function. The form can invoke the imported function while its implementation remains on the server.
Define mutations or other server-only operations in a reusable module.
Call a server function from a Client Component without shipping its implementation to the browser.
Attach a server function to a form action in a Server or Client Component.
use server do?use server marks a function or module for server execution. At module scope it applies to all exports; inside a function it marks that asynchronous function as a Server Function.
Follow-up: How can a Client Component use one? Import it from a dedicated module whose top-level directive is use server; the client receives a callable server reference rather than the implementation.
A module-level directive marks every exported function in that file, while an inline directive scopes the behavior to one function. The inline form is useful when defining a Server Function in a Server Component.
Follow-up: What restriction applies to the module-level form? Every covered export must be asynchronous.
Server Functions invoked from Client Components serialize arguments and return values across the network. Therefore, both sides must use values supported by the serialization mechanism.
Follow-up: What architectural consequence follows? The client does not receive the function body; it receives a reference that invokes the server implementation.
use clientServer FunctionsServer ActionsClient ComponentsServer Components