HaoAws/WINDOWS_BUILD_GUIDE.md
2025-09-16 16:37:48 +08:00

4.2 KiB
Raw Blame History

Windows 静态构建指南

🚨 常见权限问题解决方案

方法一:使用管理员权限

  1. 右键点击 PowerShell 或 CMD
  2. 选择"以管理员身份运行"
  3. 导航到项目目录:cd "E:\成品的网站\kejiyun"
  4. 运行构建:npm run build:static

方法二:修改杀毒软件设置

  1. 临时关闭实时保护Windows Defender 或其他杀毒软件)
  2. 将项目目录添加到杀毒软件白名单
  3. 重新尝试构建

方法三:使用简化构建命令

# 方法 1
npm run build:simple

# 方法 2
npx next build

# 方法 3如果上面的失败
set NEXT_DISABLE_TELEMETRY=1
set NODE_ENV=production
npx next build

🔧 手动构建步骤

1. 清理环境

# 停止所有Node进程
Get-Process node -ErrorAction SilentlyContinue | Stop-Process -Force

# 清理缓存目录
Remove-Item -Path ".next", "out" -Recurse -Force -ErrorAction SilentlyContinue

# 清理npm缓存
npm cache clean --force

2. 检查文件权限

# 检查当前目录权限
Get-Acl . | Format-List

# 如果需要,修改目录权限
icacls . /grant $env:USERNAME:F /t

3. 重新安装依赖

# 删除node_modules
Remove-Item -Path "node_modules" -Recurse -Force -ErrorAction SilentlyContinue

# 重新安装
npm install

4. 构建

npm run build:static

🚀 成功构建后的验证

构建成功后,您应该看到:

out/
├── index.html                    # ✅ 简体中文首页
├── about/
│   └── index.html               # ✅ 关于页面
├── news/
│   ├── index.html               # ✅ 新闻列表
│   ├── ai-transformation-2024/
│   ├── cloud-security-best-practices/
│   └── company-expansion-2024/
├── products/
├── support/
├── zh-TW/                       # ✅ 繁体中文版本
├── en/                          # ✅ 英文版本
└── _next/                       # ✅ 静态资源

🌐 本地预览

# 启动静态服务器
npm run serve

# 或者使用Python如果安装了
python -m http.server 3000 --directory out

# 或者使用其他静态服务器
npx http-server out -p 3000

🔍 故障排除

错误EPERM: operation not permitted

原因:文件权限或进程锁定问题

解决方案

  1. 以管理员身份运行终端
  2. 确保没有其他进程正在使用项目文件
  3. 临时关闭杀毒软件
  4. 重启电脑后重试

错误:构建过程中卡住

解决方案

  1. Ctrl+C 停止进程
  2. 运行:npm run build:simple
  3. 或使用 npx next build --debug

错误out目录为空或缺少文件

检查

  1. 查看构建日志中的错误信息
  2. 确认 generateStaticParams 函数正确
  3. 检查是否有动态导入或服务器端功能

错误:语言切换不工作

这是正常的,静态导出模式下:

  • 中间件不会执行
  • 语言切换依赖客户端JavaScript
  • 需要在浏览器中测试

📦 部署选项

1. Netlify推荐

# 方法1拖拽部署
# 将整个 out/ 目录拖拽到 https://app.netlify.com/drop

# 方法2命令行部署
npm install -g netlify-cli
netlify deploy --prod --dir=out

2. Vercel

npm install -g vercel
vercel --prod

3. GitHub Pages

# 推送 out/ 内容到 gh-pages 分支
git subtree push --prefix out origin gh-pages

4. 传统服务器

  • 上传 out/ 目录到服务器的网站根目录
  • 确保服务器支持SPA路由对于多语言URL

性能优化

构建后的静态网站已经包含:

  • 代码分割
  • 资源压缩
  • 图片优化
  • CSS/JS 最小化
  • 预加载关键资源

🔄 更新流程

当您修改内容后:

  1. 更新相应的Markdown或JSON文件
  2. 如果添加新文章,更新 generateStaticParams
  3. 重新运行 npm run build:static
  4. 重新部署 out/ 目录

📞 需要帮助?

如果仍然遇到问题:

  1. 确保使用管理员权限
  2. 检查Windows版本兼容性
  3. 尝试在不同的文件夹位置重新克隆项目
  4. 考虑使用WSLWindows Subsystem for Linux

记住静态导出的网站速度极快完全不需要Node.js服务器可以部署到任何静态网站托管服务