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

AWS Amplify添加isolatedModules后构建失败,需集成Cypress测试

AWS Amplify集成Cypress后构建失败(isolatedModules类型导出错误)

问题概述

  • 预期目标:在AWS Amplify CI/CD流水线中集成Cypress实现前端自动化测试
  • 问题现象:添加Cypress后,Amplify Code Build无法完成前端构建;移除测试相关配置后构建恢复正常,本地执行npm run build也出现相同错误
  • 核心错误:
Type error: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.

错误来自amplify/backend/node_modules/@aws-amplify/graphql-transformer-interfaces/src/index.ts,原因是该包在导出类型时未使用export type语法,而Next.js自动开启了isolatedModules(SWC/Babel编译要求)。

解决方案

1. 让Next.js排除amplify/backend目录的编译

在项目根目录的next.config.js中添加webpack配置,排除后端目录的TypeScript编译:

module.exports = {
  webpack: (config) => {
    // 排除amplify/backend目录,避免Next.js编译后端依赖的TS文件
    config.module.rules.forEach(rule => {
      if (rule.test && rule.test.test('.ts')) {
        rule.exclude = [...(rule.exclude || []), /amplify\/backend/];
      }
    });
    return config;
  },
};

2. 调整tsconfig.json的检查范围

更新tsconfig.json的exclude字段,明确排除amplify/backend目录,避免TypeScript检查后端依赖:

{
  "compilerOptions": {
    "isolatedModules": true,
    // 其他原有配置保持不变
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules", "amplify/backend", ".next", "out"]
}

3. 确认Amplify构建配置

确保amplify.yml中前端构建命令只处理前端代码,Cypress测试在构建完成后执行:

version: 1
frontend:
  phases:
    preBuild:
      commands:
        - npm install
    build:
      commands:
        - npm run build
        - npm run cypress:run # 执行Cypress无头测试(根据你的脚本调整)
  artifacts:
    baseDirectory: .next
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*

4. 升级AWS Amplify相关依赖

尝试升级aws-amplify和@aws-amplify/cli-extensibility-helper到最新兼容版本,新版本可能修复了TypeScript导出的规范问题:

npm update aws-amplify @aws-amplify/cli-extensibility-helper

原理说明

当Next.js使用SWC或Babel编译TypeScript时,必须开启isolatedModules选项,该选项要求所有类型导出必须使用export type语法。而amplify后端依赖的@aws-amplify/graphql-transformer-interfaces包未遵循此规范,导致编译报错。通过排除后端目录的编译范围,让Next.js只处理前端业务代码,就能绕过这个第三方包的语法问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 09:20:33