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 showFix it fast
Most common cause, 1 of 3The 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.
'use client'
export default function SaveButton() {
return (
<button onClick={() => { console.log('clicked'); alert('Saved') }}>
Save
</button>
)
}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.
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.
head -n 3 app/page.tsx
grep -n "removeConsole" next.config.*Then find the line you see in this list and open its fix:
- 1The log is in a Server Component
The file has no 'use client' line, and the message appears in the terminal where next dev runs - 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 - 3compiler.removeConsole deletes the calls from the build
next.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 consoleReferenceError: alert is not definedA 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.
npm run dev # load the page, then read this terminal windowFix
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.
'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.
npm run dev # open the page in the browser, then click SaveConfirmed by nextjs.org
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.
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.
'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.
npm run devConfirmed by nextjs.org
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.
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.
// 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.
npm run build && npm run startConfirmed by nextjs.org
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.