# 错误：调用了 notFound()，但最近的错误边界是客户端组件。notFound() 必须在服务器组件或服务器端函数中调用。

- **ID:** `nextjs/not-found-in-client-component`
- **领域:** nextjs
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

从 'next/navigation' 调用的 notFound() 函数在客户端组件或缺乏服务器端请求处理管道的上下文中被调用，导致错误边界是客户端组件而非服务器端的 not-found 页面。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 14.2.0 | active | — | — |
| 15.0.0 | active | — | — |

## 解决方案

1. ```
   Move the notFound() call to a Server Component that wraps the Client Component. Pass a prop (e.g., notFound flag) from the server to the client.
   ```
2. ```
   If you must trigger not-found from a Client Component, use a server action that returns a redirect to the not-found page, or use the error.js file pattern with notFound() in the parent layout.
   ```

## 无效尝试

- **Wrapping the Client Component in a try-catch to catch notFound()** — notFound() throws a special NEXT_NOT_FOUND error that cannot be caught by regular try-catch blocks. It must be handled by the Next.js routing layer. (90% 失败率)
- **Importing notFound from 'next/client' (which doesn't exist)** — There is no 'next/client' module. The function only exists in 'next/navigation' and its behavior is server-side only. (100% 失败率)
- **Using notFound() inside a useEffect** — notFound() is meant for synchronous server-side execution. Calling it asynchronously in useEffect doesn't trigger the server-side not-found page. (95% 失败率)
