Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

console.log and alert do nothing in a Next.js App Router page

Your error

console.log in app/page.tsx prints nothing in the browser DevTools console2 more lines it can show

Fix it fast

Most common cause, 1 of 3

The log is in a Server Component

Leave server logs where they are and read them in the terminal, or in your host's server logs after deploy.

Fix
'use client'

export default function SaveButton() {
  return (
    <button onClick={() => { console.log('clicked'); alert('Saved') }}>
      Save
    </button>
  )
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

A Next.js App Router page or layout is a Server Component unless it starts with 'use client', so its console.log output prints in the terminal running next dev, and alert does not exist there. 'use server' functions also run there. Move browser code into a 'use client' file and check next.config for compiler.removeConsole. Applies to Next.js 15 and 16.

  • Applies to Next.js 16.4, Next.js 15, React 19.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Open the file that logs and read its first line, then search the config. No 'use client' at the top means the code runs on the server (cause 1, or cause 2 for a 'use server' file). A removeConsole hit points to cause 3.

Run this first
head -n 3 app/page.tsx
grep -n "removeConsole" next.config.*

Then find the line you see in this list and open its fix:

  1. 1The log is in a Server ComponentThe file has no 'use client' line, and the message appears in the terminal where next dev runs
  2. 2The code is inside a Server Function marked 'use server'The log sits in a function from a 'use server' file or block, and in development the terminal shows the call as a POST line naming that function
  3. 3compiler.removeConsole deletes the calls from the buildnext.config.js, .mjs or .ts has a compiler.removeConsole entry, and the logs are missing in the terminal as well as the browser

What does the error look like?

  • console.log in app/page.tsx prints nothing in the browser DevTools console
  • ReferenceError: alert is not defined
  • A log inside a form action or 'use server' function never shows up in the browser

Cause 1 of 3

The log is in a Server Component

You seeThe file has no 'use client' line, and the message appears in the terminal where next dev runs

Check

When console.log from a Next.js page never reaches the browser, the usual reason is that the page is a Server Component. Without a 'use client' line at the top, an App Router file is a Server Component, and the Next.js boundary guide marks that kind of component as running on the server but not in the browser. Browser functions such as alert and window are missing in that environment.

Check
npm run dev   # load the page, then read this terminal window

Fix

Leave server logs where they are and read them in the terminal, or in your host's server logs after deploy. For output in the browser, put the code in a small Client Component that starts with 'use client', and call alert from an event handler rather than while the component renders.

Fix
'use client'

export default function SaveButton() {
  return (
    <button onClick={() => { console.log('clicked'); alert('Saved') }}>
      Save
    </button>
  )
}

Verify

Clicking the button prints 'clicked' in the browser console and opens the alert box.

Verify
npm run dev   # open the page in the browser, then click Save

Confirmed by nextjs.org

Did this fix it?

Cause 2 of 3

The code is inside a Server Function marked 'use server'

You seeThe log sits in a function from a 'use server' file or block, and in development the terminal shows the call as a POST line naming that function

Check

A console.log or alert inside a Server Function cannot show in the browser, because the 'use server' directive makes the function execute on the server side. That stays true when a Client Component calls it from a button or a form.

Check
grep -rn "'use server'" app/

Fix

Read that output in the terminal. To show the user a result, return a small value from the Server Function and display it in the Client Component once the call resolves.

Fix
'use client'
import { saveNote } from './actions'

export default function SaveNote() {
  return (
    <button onClick={async () => {
      const result = await saveNote()
      alert(result.message)
    }}>
      Save note
    </button>
  )
}

Verify

The alert shows the message returned by saveNote, and the server-side log still prints in the terminal.

Verify
npm run dev

Confirmed by nextjs.org

Did this fix it?

Cause 3 of 3

compiler.removeConsole deletes the calls from the build

You seenext.config.js, .mjs or .ts has a compiler.removeConsole entry, and the logs are missing in the terminal as well as the browser

Check

The Next.js compiler removes every console.* call from application code (node_modules excluded) when compiler.removeConsole is set. The calls no longer exist in the output, so nothing prints anywhere.

Check
grep -n -A3 "removeConsole" next.config.*

Fix

Delete the option to get every log back, or keep the stripping and exclude console.error so real errors survive.

Fix
// next.config.js
module.exports = {
  compiler: {
    removeConsole: {
      exclude: ['error'],
    },
  },
}

Verify

After a fresh build, console.error calls print again while plain console.log stays stripped.

Verify
npm run build && npm run start

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • Keep 'use client' on the small interactive file that needs alert, window or click handlers, and let pages stay Server Components.
  • On Next.js 16.2 or later, set logging.browserToTerminal to true while developing, so browser logs print in the same terminal as server logs.
  • If production builds strip logs, add exclude: ['error'] so failures still leave a trace.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, QistLock, Taskvare.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.