You need to enable JavaScript to run this app. TypeScript项目升级apollo-server系列依赖至2.12.0版本遇TS1005编译报错
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript项目升级apollo-server系列依赖至2.12.0版本遇TS1005编译报错

Apollo Server 版本升级TypeScript编译异常排查

问题描述

  • 操作场景:TypeScript项目将apollo-server、apollo-server-express依赖从^2.9.4版本升级至2.12.0版本后,构建流程触发编译失败
  • 报错定位:错误位置为node_modules/apollo-server-express/node_modules/apollo-server-core/dist/plugin/index.d.ts第1行第13列,错误类型为TS1005: '=' expected
  • 报错关联代码:
import type { ApolloServerPlugin } from 'apollo-server-plugin-base';
  • 已验证的无效操作:删除node_modules目录、package-lock.json文件后重新安装依赖,问题无改善

项目现有配置

tsconfig.json 配置

{
  "compilerOptions": {
    "target": "es5",
    "moduleResolution": "node",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "outDir": "./dist",
    "declaration": true
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts",
    "**/*.test.ts"
  ]
}

package.json 核心配置

{
  "dependencies": {
    "@apollo/federation": "^0.10.2",
    "@types/bluebird": "^3.5.27",
    "@types/cookie-parser": "^1.4.2",
    "@types/jest": "^24.0.19",
    "@types/mysql": "^2.15.7",
    "@types/nanoid": "^2.0.0",
    "@types/node": "^14.10.1",
    "@types/umzug": "^2.2.2",
    "@types/validator": "^10.11.3",
    "apollo-cache-control": "^0.10.0",
    "apollo-cache-inmemory": "^1.6.6",
    "apollo-client": "^2.6.4",
    "apollo-datasource-rest": "^0.6.1",
    "apollo-link": "^1.2.12",
    "apollo-link-context": "^1.0.20",
    "apollo-link-http": "^1.5.17",
    "apollo-link-rest": "^0.7.3",
    "apollo-link-schema": "^1.2.3",
    "apollo-server": "^2.12.0",
    "apollo-server-express": "^2.12.0",
    "apollo-server-plugin-response-cache": "^0.3.1",
    "apollo-server-testing": "^2.9.0",
    "axios": "^0.19.2",
    "cookie-parser": "^1.4.4",
    "cron": "^1.7.2",
    "elastic-apm-node": "^3.3.0",
    "express": "^4.17.1",
    "express-graphql": "^0.9.0",
    "graphql": "^14.5.8",
    "graphql-import": "^0.7.1",
    "graphql-schema-linter": "^0.2.1",
    "jsonwebtoken": "^8.5.1",
    "jwk-to-pem": "^2.0.3",
    "kafka-node": "^4.1.3",
    "link": "^0.1.5",
    "log4js": "^5.0.0",
    "mysql2": "^1.7.0",
    "nanoid": "^3.1.7",
    "node": "^14.5.0",
    "node-cache": "^5.1.1",
    "node-fetch": "^2.6.0",
    "promise-retry": "^1.1.1",
    "rimraf": "^3.0.0",
    "save": "^2.4.0",
    "sequelize": "^5.19.5",
    "sequelize-cli": "^5.5.1",
    "soap": "^0.30.0",
    "ts-jest": "^24.1.0",
    "typescript": "^3.6.3",
    "umzug": "^2.2.0"
  },
  "devDependencies": {
    "@types/graphql": "^14.5.0",
    "graphql-tag": "^2.10.1",
    "graphql-tools": "^4.0.5",
    "jest": "^24.8.0",
    "jest-cli": "^24.8.0"
  },
  "jest": {
    "testEnvironment": "node",
    "testPathIgnorePatterns": [
      "/node_modules/",
      "/dist/"
    ]
  }
}

根因分析

  • 报错核心原因是项目使用的TypeScript版本不兼容Apollo Server 2.12.0版本的语法特性
  • import type是TypeScript 3.8版本才正式引入的类型专用导入语法,当前项目锁定的TypeScript版本为^3.6.3,内置语法解析器无法识别该关键字,因此在解析Apollo附带的类型定义文件时抛出TS1005语法错误
  • 项目配置的skipLibCheck: true无法规避该问题:该配置仅能跳过对依赖库的类型校验逻辑,无法在语法解析阶段拦截低版本TS不支持的语法

修复指引

  • 方案一:升级TypeScript版本至3.8及以上,将typescript依赖版本调整为至少^3.8.0,重新安装依赖即可正常解析import type语法
  • 方案二:如果暂时无法升级TypeScript大版本,可将apollo-server、apollo-server-express版本回退至适配TypeScript 3.6.x语法支持范围的版本区间

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.20 21:24:02