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

NestJS集成Google登录报错:无法导出未包含的Provider/Module

问题分析与解决方案

错误原因

  1. GoogleModule导出配置错误:你在GoogleModule的providers中仅通过自定义令牌'GOOGLE_SERVICE'注册了GoogleService,但exports数组直接写了GoogleService类。Nest无法识别这个导出项,因为当前模块的providers中没有直接注册该类。
  2. AppModule重复注册组件:你在AppModule的controllers和providers中重复声明了GoogleController、GoogleService、GoogleStrategy,这些组件已经属于GoogleModule,无需在根模块再次注册,否则会导致依赖注入混乱。

修复步骤

1. 修正GoogleModule

将GoogleService直接加入providers数组,确保Nest能识别要导出的服务:

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { GoogleService } from './google.service';
import { GoogleStrategy } from './google.strategy';
import { GoogleController } from './google.controller';
import { User } from 'src/user/entities/user.entity';
import { SessionSerializer } from '../Serializer';

@Module({
  imports: [TypeOrmModule.forFeature([User])],
  controllers: [GoogleController],
  providers: [
    GoogleStrategy,
    SessionSerializer,
    GoogleService, // 直接注册GoogleService类
    {
      provide: 'GOOGLE_SERVICE',
      useClass: GoogleService, // 保留别名(如果业务需要该令牌注入)
    },
  ],
  exports: [GoogleService], // 现在providers中存在该类,可正常导出
})
export class GoogleModule {}

如果不需要自定义令牌,也可以简化为:

@Module({
  imports: [TypeOrmModule.forFeature([User])],
  controllers: [GoogleController],
  providers: [GoogleStrategy, SessionSerializer, GoogleService],
  exports: [GoogleService],
})
export class GoogleModule {}

2. 修正AppModule

移除重复注册的GoogleController、GoogleService、GoogleStrategy:

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from '@nestjs/config';
import { PassportModule } from '@nestjs/passport';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UserModule } from './user/user.module';
import { GoogleModule } from './google/google.module';
import { FacebookStrategy } from './facebook/facebook.strategy';
import { User, Program, Statistic, Exercise } from './user/entities';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'postgres',
      username: process.env.DB_USERNAME,
      password: process.env.DB_PASSWORD,
      database: process.env.DB_DATABASE,
      host: process.env.DB_HOST,
      port: parseInt(process.env.DB_PORT),
      synchronize: true,
      autoLoadEntities: true,
      entities: [User, Program, Statistic, Exercise],
    }),
    ConfigModule.forRoot(),
    UserModule,
    GoogleModule,
    PassportModule.register({ session: true }),
  ],
  controllers: [AppController], // 移除重复的GoogleController
  providers: [AppService, FacebookStrategy], // 移除重复的GoogleService和GoogleStrategy
})
export class AppModule {}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 23:37:50