错误排查
本文档整理了 Mizuki 主题使用过程中可能遇到的常见错误和问题,以及相应的解决方案。
安装和依赖问题
Section titled “安装和依赖问题”Error: Cannot find module ‘xxx’
Section titled “Error: Cannot find module ‘xxx’”原因:缺少依赖包
解决方案:
# 清除缓存并重新安装rm -rf node_modules pnpm-lock.yamlpnpm install
# 如果是特定包缺失pnpm install xxxError: ERR_PNPM_PEER_DEP_ISSUES
Section titled “Error: ERR_PNPM_PEER_DEP_ISSUES”原因:依赖版本冲突
解决方案:
# 方法 1:忽略 peer 依赖pnpm install --no-strict-peer-dependencies
# 方法 2:使用 --forcepnpm install --force
# 方法 3:检查并更新依赖pnpm outdatedpnpm updateError: EACCES permission denied
Section titled “Error: EACCES permission denied”原因:权限不足
解决方案:
# Linux/Macsudo chown -R $(whoami) ~/.pnpm-storesudo chown -R $(whoami) node_modules
# Windows# 以管理员身份运行 PowerShell开发服务器问题
Section titled “开发服务器问题”Error: listen EADDRINUSE: address already in use
Section titled “Error: listen EADDRINUSE: address already in use”原因:端口被占用
解决方案:
# 查找占用端口的进程# Linux/Maclsof -i :4321
# Windowsnetstat -ano | findstr :4321
# 杀死进程kill -9 <PID> # Linux/Mactaskkill /PID <PID> /F # Windows
# 或使用其他端口pnpm dev --port 3000Error: Module not found
Section titled “Error: Module not found”原因:模块路径错误或模块不存在
解决方案:
# 检查导入路径是否正确# 检查模块是否安装pnpm list
# 清除缓存rm -rf .astro node_modulespnpm installError: Unexpected token ‘<’
Section titled “Error: Unexpected token ‘<’”原因:HTML 文件被错误地作为 JavaScript 处理
解决方案:
- 检查文件扩展名是否正确
- 检查导入语句是否正确
- 检查 Astro 组件语法
Error: Build failed
Section titled “Error: Build failed”原因:构建过程中出现错误
解决方案:
# 查看详细错误信息pnpm build --verbose
# 清除构建缓存rm -rf dist .astropnpm buildError: JavaScript heap out of memory
Section titled “Error: JavaScript heap out of memory”原因:内存不足
解决方案:
# 增加 Node.js 内存限制NODE_OPTIONS="--max-old-space-size=4096" pnpm build
# 或在 package.json 中设置{ "scripts": { "build": "NODE_OPTIONS='--max-old-space-size=4096' astro build" }}Error: Static page generation failed
Section titled “Error: Static page generation failed”原因:静态页面生成失败
解决方案:
- 检查页面代码是否有错误
- 检查数据获取是否正确
- 检查 Markdown 文件格式
内容分离问题
Section titled “内容分离问题”Error: 未启用内容分离功能
Section titled “Error: 未启用内容分离功能”原因:ENABLE_CONTENT_SYNC 未设置或设置为 false
解决方案:
# 检查 .env 文件cat .env
# 确保有以下配置ENABLE_CONTENT_SYNC=trueCONTENT_REPO_URL=https://github.com/your-username/Mizuki-Content.gitError: 未设置 CONTENT_REPO_URL
Section titled “Error: 未设置 CONTENT_REPO_URL”原因:启用了内容分离但未配置仓库地址
解决方案:
# 在 .env 中添加CONTENT_REPO_URL=https://github.com/your-username/Mizuki-Content.gitError: Git clone failed
Section titled “Error: Git clone failed”原因:Git 克隆失败,可能是网络问题或权限问题
解决方案:
# 测试 Git 连接git ls-remote https://github.com/your-username/Mizuki-Content.git
# 如果是私有仓库,确保配置了 SSH 密钥或 Tokenssh -T git@github.com
# 或使用 HTTPS + TokenCONTENT_REPO_URL=https://YOUR_TOKEN@github.com/your-username/Mizuki-Content.gitError: 404 Not Found
Section titled “Error: 404 Not Found”原因:部署配置错误或文件路径问题
解决方案:
- 检查部署平台的构建配置
- 确保输出目录正确(通常是
dist) - 检查路由配置
// Vercel 配置{ "buildCommand": "pnpm build", "outputDirectory": "dist"}Error: Environment variable not found
Section titled “Error: Environment variable not found”原因:环境变量未配置
解决方案:
- 在部署平台添加环境变量
- 确保变量名正确
- 重新部署
Error: Build timeout
Section titled “Error: Build timeout”原因:构建时间过长
解决方案:
- 优化构建配置
- 使用缓存
- 升级部署平台的套餐
API 问题
Section titled “API 问题”Error: Bangumi API 401 Unauthorized
Section titled “Error: Bangumi API 401 Unauthorized”原因:Access Token 无效或过期
解决方案:
# 检查 Token 是否正确echo $BANGUMI_ACCESS_TOKEN
# 重新获取 Tokencurl -X POST "https://bgm.tv/oauth/access_token" \ -H "Content-Type: application/json" \ -d '{ "grant_type": "refresh_token", "client_id": "YOUR_CLIENT_ID", "client_secret": "YOUR_CLIENT_SECRET", "refresh_token": "YOUR_REFRESH_TOKEN" }'Error: Meting API 无法访问
Section titled “Error: Meting API 无法访问”原因:API 服务不可用或跨域问题
解决方案:
- 检查 API 服务是否正常
- 使用自建 Meting API
- 配置代理
export const musicConfig = { api: 'http://your-server:3000/', // 自建 API};图片和媒体问题
Section titled “图片和媒体问题”Error: 图片不显示
Section titled “Error: 图片不显示”原因:图片路径错误或图片不存在
解决方案:
# 检查图片是否存在ls -la public/images/posts/
# 检查图片路径是否正确# 在 Markdown 中使用正确的路径Error: 图片加载缓慢
Section titled “Error: 图片加载缓慢”原因:图片文件过大或网络问题
解决方案:
- 压缩图片
- 使用 WebP 格式
- 使用懒加载
- 使用 CDN
# 压缩图片npx sharp-cli -i 'public/images/**/*.jpg' -o 'public/images/compressed' --quality 80
# 转换为 WebPnpx sharp-cli -i 'public/images/**/*.jpg' -o 'public/images/webp' --format webp样式和布局问题
Section titled “样式和布局问题”Error: 样式不生效
Section titled “Error: 样式不生效”原因:CSS 优先级或选择器错误
解决方案:
- 检查选择器是否正确
- 检查 CSS 是否被覆盖
- 使用浏览器开发者工具调试
/* 提高优先级 */.my-component .title { color: red !important;}Error: 布局错乱
Section titled “Error: 布局错乱”原因:HTML 结构或 CSS 布局问题
解决方案:
- 检查 HTML 结构
- 检查 CSS 布局
- 使用浏览器开发者工具调试
Error: 搜索功能不工作
Section titled “Error: 搜索功能不工作”原因:Pagefind 索引未生成或配置错误
解决方案:
# 重新构建搜索索引pnpm build
# 检查 Pagefind 配置cat pagefind.yml
# 确保搜索索引文件存在ls -la dist/pagefind/Error: 搜索结果不准确
Section titled “Error: 搜索结果不准确”原因:搜索索引需要更新
解决方案:
# 清除旧索引并重新构建rm -rf dist/pagefindpnpm build评论系统问题
Section titled “评论系统问题”Error: 评论加载失败
Section titled “Error: 评论加载失败”原因:评论系统配置错误或服务不可用
解决方案:
- 检查评论系统配置
- 检查服务是否正常
- 检查网络连接
export const commentConfig = { twikoo: { enabled: true, envId: 'your-env-id', // 确保配置正确 },};页面加载缓慢
Section titled “页面加载缓慢”原因:资源文件过大或请求过多
解决方案:
- 优化图片
- 启用压缩
- 使用缓存
- 减少请求
export default defineConfig({ vite: { build: { rollupOptions: { output: { manualChunks: { vendor: ['react', 'react-dom'], }, }, }, }, },});内存占用过高
Section titled “内存占用过高”原因:内存泄漏或资源未释放
解决方案:
- 检查是否有内存泄漏
- 优化代码
- 增加服务器内存
使用浏览器开发者工具
Section titled “使用浏览器开发者工具”- 打开浏览器开发者工具(F12)
- 查看 Console 面板的错误信息
- 查看 Network 面板的请求状态
- 查看 Elements 面板的 HTML 结构
查看构建日志
Section titled “查看构建日志”# 查看详细构建日志pnpm build --verbose
# 查看开发服务器日志pnpm dev --verbose使用调试模式
Section titled “使用调试模式”# 启用调试模式DEBUG=* pnpm dev
# 或特定模块DEBUG=astro:* pnpm dev如果以上方法都无法解决问题:
- 搜索问题:在 GitHub Issues 中搜索
- 提交 Issue:提供详细的错误信息
- 社区讨论:在 GitHub Discussions 中讨论
# 收集环境信息node -vpnpm -vcat package.jsoncat .env💡 提示:遇到问题时不要慌张,仔细阅读错误信息,按照步骤排查,通常都能找到解决方案。