Skip to main content

What does the "use server" directive mean?

The "use server" directive is a special instruction for Next.js that explicitly says: "This code must run only on the server".

It is needed so Next.js can unambiguously understand the boundary between the client and the server.


What "use server" does

When Next.js sees "use server":

  • the code never reaches the browser
  • the function runs only on the server
  • it can be called:
    • from Server Components
    • from Client Components
    • from HTML forms

In effect, it's a marker:

"This function must not run on the client."


Where you can use "use server"

1. At the top of a file

If the directive is the first line of the file:

ts
'use server' export async function createUser() { // server code }

All exported functions in that file are considered server functions.


2. Before a specific function

ts
export async function deleteUser() { 'use server' // server code }

Only that function becomes a Server Action.


Why this directive is needed at all

1. Explicit separation of the execution environment

In Next.js there is:

  • Server Components
  • Client Components
  • shared code

Without the directive, the framework cannot safely guess exactly where the code should run.

"use server" removes the ambiguity.


2. Security

With code under "use server" you can:

  • access a database
  • use secrets and tokens
  • work with the file system
  • call private APIs

And at the same time be sure that this code won't leak into the client.


3. The ability to call a server function from the UI

Without "use server" a function:

  • would either become ordinary server logic
  • or wouldn't be callable from a client component at all

The directive makes the function available as a Server Action.


What "use server" does NOT do

Important not to confuse:

  • it is not middleware
  • it is not authorization protection
  • it is not access restriction by itself
  • it is not a runtime check

If a user can invoke the action from the UI, you still must check permissions, data, and state.


Difference from "use client"

"use server""use client"
Code only on the serverCode in the browser
No access to the DOMHas access to the DOM
Secrets can be usedSecrets cannot be used
Server ActionsReact hooks, events

Both directives are needed for a clear separation of responsibility.


In short

"use server" is a directive that marks a function or a file as server code, guaranteeing that it runs only on the server and can be used as a Server Action. It is needed for security, clarity, and the correct operation of Next.js's architecture.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.