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

使用nestjs-i18n做国际化时出现FALLBACK_LANGUAGE配置键不存在错误

解决nestjs-i18n中「FALLBACK_LANGUAGE配置项不存在」的错误

错误信息

{"message":"[App] Configuration key \"FALLBACK_LANGUAGE\" does not exist"}

问题代码(app.module.ts)

import { Module } from '@nestjs/common'
import { ConfigService as NestConfig } from '@nestjs/config'
import * as path from 'path';
import { I18nModule, AcceptLanguageResolver, QueryResolver, HeaderResolver } from 'nestjs-i18n';

@Module({
  imports: [
    I18nModule.forRootAsync({
      useFactory: (configSrv: NestConfig) => ({
        fallbackLanguage: configSrv.getOrThrow('FALLBACK_LANGUAGE'),
        loaderOptions: {
          path: path.join(__dirname, '/i18n/'),
          watch: true,
        },
      }),
      resolvers: [
        { use: QueryResolver, options: ['lang'] },
        AcceptLanguageResolver,
        new HeaderResolver(['x-lang']),
      ],
      inject: [NestConfig],
    })
  ],
  providers: [],
})
export class AppModule {}

解决方案

  • 检查环境变量配置:确认项目根目录的.env文件(或系统环境变量)中已定义FALLBACK_LANGUAGE,示例:
    FALLBACK_LANGUAGE=zh-CN
    
  • 确保ConfigModule已导入:代码依赖@nestjs/config的ConfigService,必须在AppModule的imports中添加ConfigModule配置,示例:
    import { ConfigModule } from '@nestjs/config';
    
    @Module({
      imports: [
        ConfigModule.forRoot({ isGlobal: true }), // 全局导入避免重复配置
        I18nModule.forRootAsync(...)
      ]
    })
    
  • 验证配置键名一致性:FALLBACK_LANGUAGE大小写敏感,确保.env中的键名和代码中getOrThrow的参数完全一致,无拼写或大小写错误。
  • 确认依赖安装完整:执行npm install @nestjs/config nestjs-i18n,确保两个依赖都正确安装且版本兼容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 02:42:22