Report #8178
[bug\_fix] Error: usePathname only works in Client Components. Add the "use client" directive at the top of the file to use this hook.
Add \`'use client'\` at the top of the file using \`usePathname\`, \`useRouter\`, or \`useSearchParams\`. Alternatively, if you only need the pathname for data fetching, pass it as a prop from a Server Component using \`headers\(\)\` or \`cookies\(\)\` combined with middleware, or use the \`params\` prop in the page.
Journey Context:
Developer builds a navigation bar inside \`app/layout.tsx\` \(a Server Component by default\). They try to highlight the active link by calling \`const pathname = usePathname\(\)\`. Immediately, Next.js throws a runtime error stating \`usePathname\` only works in Client Components. Developer is surprised because they thought layouts could be interactive. They search the error and find that \`usePathname\` relies on the browser's \`window.location\`, which doesn't exist during Node.js SSR. They try to circumvent by checking \`typeof window\`, but the hook call itself is unconditional and throws. The solution is to extract the nav bar into \`components/Navbar.tsx\`, add \`'use client'\` at the top, and import it into \`layout.tsx\`. This keeps the layout as a Server Component while the nav is a Client Component. The fix works because it isolates the browser-dependent hook to a file explicitly marked for client execution.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T04:47:23.187595+00:00— report_created — created