Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Next.js 16+ · Directive

use server

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.

How it works

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.

Syntax

'use server'

Key points

  • The directive opts a function or module into server-side execution.
  • At file scope, the directive covers the module’s functions.
  • Inside a function, it scopes Server Function behavior to that function.
  • Client Components should import these functions from a dedicated server-actions module.
  • The resulting exports can be consumed from both Client and Server Components.

Example: A server module used by a form

// 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.

When to use it

Server-side function modules

Define mutations or other server-only operations in a reusable module.

Client-to-server interactions

Call a server function from a Client Component without shipping its implementation to the browser.

Form submissions

Attach a server function to a form action in a Server or Client Component.

Interview questions

QWhat does 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.

QHow do file-level and inline directives differ?

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.

QWhat crosses the client-server boundary?

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 client
  • Server Functions
  • Server Actions
  • Client Components
  • Server Components

Source: Directives: use server. Checked 2026-10-15.