Skill站外收录 @skilllib/skill-2117939930272413548
nextjs-server-side-error-debugging
专门定位 Next.js 服务端渲染和数据获取阶段的静默错误,通过终端日志捕获完整堆栈信息,识别环境变量缺失、数据库连接失败、序列化异常等根本原因,避免被浏览器空控制台或泛化错误页误导。
软件开发 AI 技能
Skills.MD
# Next.js Server-Side Error Debugging
## Problem
Server-side errors in Next.js don't appear in the browser console, making debugging
frustrating when you're looking in the wrong place. The browser shows a generic error
page or 500 status, but no stack trace or useful error information appears in DevTools.
## Context / Trigger Conditions
This skill applies when:
- Page displays "Internal Server Error" or custom error page
- Browser console shows no errors, or only a generic fetch failure
- You're using `getServerSideProps`, `getStaticProps`, or API routes
- Error only occurs on page refresh or direct navigation (not client-side transitions)
- The error is intermittent and hard to reproduce in the browser
Common misleading symptoms:
- "Unhandled Runtime Error" modal that doesn't show the real cause
- Network tab shows 500 but response body is empty or generic
- Error disappears when you add console.log (timing issue)
## Solution
### Step 1: Check the Terminal
The actual error with full stack trace appears in the terminal where `npm run dev`
or `next dev` is running. This is the **first place to look**.
```bash
# If you don't see the terminal, find the process
ps aux | grep next
# Or restart with visible output
npm run dev
```
### Step 2: Add Explicit Error Handling
For persistent debugging, wrap server-side code with try-catch:
```typescript
export async function getServerSideProps(context) {
try {
const data = await fetchSomething();
return { props: { data } };
} catch (error) {
console.error('getServerSideProps error:', error);
// Return error state instead of throwing
return { props: { error: error.message } };
}
}
```
### Step 3: For Production Errors
Check your hosting provider's logs:
- **Vercel**: Dashboard → Project → Logs (Functions tab)
- **AWS**: CloudWatch Logs
- **Netlify**: Functions tab in dashboard
- **Self-hosted**: Check your Node.js process logs
### Step 4: Common Causes
1. **Environment variables**: Missing in production but present locally
2. **Database connections**: Connection string issues, cold starts
3. **Import errors**: Server-only code accidentally imported on client
4. **Async/await**: Missing await on async operations
5. **JSON serialization**: Objects that can't be serialized (dates, functions)
## Verification
After checking the terminal, you should see:
- Full stack trace with file name and line number
- The actual error message (not generic 500)
- Variable values if you added console.log statements
## Example
**Symptom**: User reports page shows "Internal Server Error" after clicking a link.
**Investigation**:
1. Open browser DevTools → Console: Empty
2. Network tab shows: `GET /dashboard → 500`
3. Check terminal running `npm run dev`:
```
Error: Cannot read property 'id' of undefined
at getServerSideProps (/app/pages/dashboard.tsx:15:25)
at renderToHTML (/app/node_modules/next/dist/server/render.js:428:22)
```
**Cause found**: Database query returned `null` instead of user object.
## Notes
- In development, Next.js sometimes shows an error overlay, but it often has less
detail than the terminal output
- `reactStrictMode: true` in `next.config.js` causes double-execution of server
functions in development, which can make debugging confusing
- For API routes, the error appears in the same terminal as page errors
- Client-side errors (in useEffect, event handlers) DO appear in browser console—
this skill only applies to server-side code
- If using `next start` (production mode locally), errors may be less verbose;
check `NODE_ENV` and consider adding custom error logging
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「nextjs-server-side-error-debugging」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次