You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在AWS Amplify部署Next.js应用失败,求排查解决方法

在AWS Amplify部署Next.js应用失败的修复方案

问题背景

部署Next.js 14.0.1应用到AWS Amplify时,构建环节报错:

Error: Cannot find module 'next/dist/build/index.js'

相关配置文件

package.json:

{
  "name": "bytecho",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@next/font": "^14.0.1",
    "next": "14.0.1",
    "react": "^18",
    "react-dom": "^18"
  },
  "devDependencies": {
    "@types/node": "^20",
    "@types/react": "^18",
    "@types/react-dom": "^18",
    "autoprefixer": "^10.0.1",
    "eslint": "^8",
    "eslint-config-next": "14.0.1",
    "postcss": "^8",
    "tailwindcss": "^3.3.0",
    "typescript": "^5"
  }
}

amplify.yml:

version: 1
frontend:
  phases:
    preBuild:
      commands:
        - npm ci
    build:
      commands:
        - next build
  artifacts:
    baseDirectory: .next
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*

修复步骤

1. 匹配Node.js版本

Next.js 14要求Node.js版本至少为18.17.0或20.6.0,Amplify默认环境的Node版本可能不满足要求,需手动指定:

  • 方式一:修改amplify.yml,在preBuild阶段添加版本切换命令:
    version: 1
    frontend:
      phases:
        preBuild:
          commands:
            - nvm install 20
            - nvm use 20
            - npm ci
        build:
          commands:
            - next build
      artifacts:
        baseDirectory: .next
        files:
          - '**/*'
      cache:
        paths:
          - node_modules/**/*
    
  • 方式二:在AWS Amplify控制台的构建设置中添加环境变量NODE_VERSION=20

2. 调整构建命令

将next build替换为npx next build,避免环境PATH导致的命令定位问题:

build:
  commands:
    - npx next build

3. 重置依赖缓存

如果上述方法无效,尝试清理Amplify的构建缓存:

  • 在Amplify控制台的构建历史中找到对应任务,点击清理构建缓存后重新触发构建
  • 或者将npm ci替换为npm install(ci模式严格依赖package-lock.json,若存在版本不兼容可能导致依赖安装不完整)

4. 验证依赖一致性

确保package.json中next版本与eslint-config-next版本完全一致(当前配置中均为14.0.1,此步骤可跳过)

内容的提问来源于stack exchange,提问作者juanserna8

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 20:23:15