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

NestJS整合Fastify/Apollo出现Fastify版本兼容错误求助

问题:NestJS Fastify/Apollo 服务器版本冲突报错

报错信息

FastifyError: fastify-plugin: fastify-accepts - expected '3.x' fastify version, '4.6.0' is installed

问题背景

按照NestJS官方文档搭建Fastify/Apollo服务器,在main.ts中配置Fastify后触发上述版本冲突错误。尝试过安装Fastify 3.x版本、调整其他模块版本,但yarn.lock显示部分模块依赖3.x、部分依赖4.x,冲突无法解决。

参考官方文档:

  • NestJS性能优化指南
  • NestJS GraphQL快速入门

相关代码

main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import {
  FastifyAdapter,
  NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { ValidationPipe } from '@nestjs/common';

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter({ logger: true }),
  );

  app.useGlobalPipes(new ValidationPipe());
  await app.listen(3000);
  console.log(`Application is running on: ${await app.getUrl()}`);
}
bootstrap();

package.json

{
  "name": "app",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/apollo": "^10.1.0",
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/graphql": "^10.1.2",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/platform-fastify": "^9.1.1",
    "apollo-server-fastify": "^3.10.2",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "firebase-admin": "^11.0.1",
    "graphql": "^16.6.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0",
    "ts-morph": "^16.0.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "28.1.8",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "28.1.3",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "28.0.8",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.1.0",
    "typescript": "^4.7.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

解决方案

核心问题

apollo-server-fastify@3.10.2依赖Fastify 3.x,而@nestjs/platform-fastify@9.1.1默认依赖Fastify 4.x,二者版本不兼容导致冲突。

方案1:统一锁定Fastify 3.x

  1. 安装指定版本的依赖,确保版本匹配:
yarn add fastify@^3.29.0 @nestjs/platform-fastify@^9.1.1 apollo-server-fastify@^3.10.2
  1. 在package.json中添加resolutions字段(Yarn专属)强制统一版本:
"resolutions": {
  "fastify": "^3.29.0",
  "fastify-accepts": "^2.1.0"
}
  1. 重新安装依赖:
yarn install --force

方案2:升级到适配Fastify 4.x的Apollo包(推荐长期方案)

apollo-server-fastify已停止维护,官方推荐使用@apollo/server搭配@as-integrations/fastify:

  1. 卸载旧包:
yarn remove @nestjs/apollo apollo-server-fastify
  1. 安装适配Fastify 4.x的新版本:
yarn add @nestjs/apollo@^11.0.0 @apollo/server@^4.0.0 @as-integrations/fastify@^2.0.0
  1. 修改AppModule中的GraphQL配置,切换到新驱动:
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';

@Module({
  imports: [
    GraphQLModule.forRoot<ApolloDriverConfig>({
      driver: ApolloDriver,
      autoSchemaFile: true,
    }),
  ],
})
export class AppModule {}

方案3:临时兼容配置(不推荐长期使用)

创建FastifyAdapter时手动注册兼容插件:

new FastifyAdapter({
  logger: true,
  pluginTimeout: 10000,
})
.register(require('fastify-accepts'), { forceCloseConnections: true })

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 11:15:41