fix: lỗi đọc nhầm .env ở thư mục backend

This commit is contained in:
2026-06-19 08:13:35 +07:00
parent 623f0164c8
commit 6f2dd0fc01
5 changed files with 88 additions and 54 deletions
+14 -25
View File
@@ -49,24 +49,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommentGateway = exports.EmailService = exports.TourRoleGuard = exports.Roles = exports.ROLES_KEY = void 0;
const dotenv = __importStar(require("dotenv"));
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const possibleEnvPaths = [
path.resolve(process.cwd(), '.env'),
path.resolve(process.cwd(), '..', '.env'),
path.resolve(__dirname, '..', '.env'),
path.resolve(__dirname, '..', '..', '.env'),
];
let loadedEnvPath = null;
for (const p of possibleEnvPaths) {
if (fs.existsSync(p)) {
dotenv.config({ path: p });
console.log(`[Config] 📂 Biến môi trường được tải từ: ${p}`);
loadedEnvPath = p;
break;
}
}
const config_1 = require("@nestjs/config");
require("reflect-metadata");
const zlib = __importStar(require("zlib"));
const util_1 = require("util");
@@ -98,12 +83,6 @@ const CACHE_TTL = {
USER_ROLE: 300000,
};
async function bootstrap() {
if (!process.env.DATABASE_URL) {
throw new Error('❌ DATABASE_URL không tồn tại trong biến môi trường! Kiểm tra file .env tại: ' + (loadedEnvPath || 'một trong các đường dẫn đã thử: ' + possibleEnvPaths.join(', ')));
}
console.log('====================================');
console.log('DATABASE_URL:', process.env.DATABASE_URL);
console.log('====================================');
const app = await core_1.NestFactory.create(AppModule);
app.setGlobalPrefix('api/v1');
app.enableCors();
@@ -293,9 +272,10 @@ AppController = __decorate([
(0, common_1.Controller)()
], AppController);
let AuthController = class AuthController {
constructor(prisma, jwtService, emailService, cacheManager) {
constructor(prisma, jwtService, configService, emailService, cacheManager) {
this.prisma = prisma;
this.jwtService = jwtService;
this.configService = configService;
this.emailService = emailService;
this.cacheManager = cacheManager;
}
@@ -395,9 +375,10 @@ __decorate([
], AuthController.prototype, "signupVerify", null);
AuthController = __decorate([
(0, common_1.Controller)('auth'),
__param(3, (0, common_1.Inject)(cache_manager_1.CACHE_MANAGER)),
__param(4, (0, common_1.Inject)(cache_manager_1.CACHE_MANAGER)),
__metadata("design:paramtypes", [prisma_service_1.PrismaService,
jwt_1.JwtService,
config_1.ConfigService,
EmailService, Object])
], AuthController);
let PublicTourController = class PublicTourController {
@@ -1818,6 +1799,11 @@ let AppModule = class AppModule {
AppModule = __decorate([
(0, common_1.Module)({
imports: [
config_1.ConfigModule.forRoot({
isGlobal: true,
envFilePath: path.resolve(__dirname, '..', '..', '.env'),
ignoreEnvFile: process.env.NODE_ENV === 'production',
}),
cache_manager_1.CacheModule.registerAsync({
isGlobal: true,
useFactory: async () => ({
@@ -1833,11 +1819,14 @@ AppModule = __decorate([
}),
],
controllers: [AppController, AuthController, PublicTourController, TourController, UserController, RoutingController, LegController, LocationController, CommentController, PhotoController, AdminOtpController],
providers: [prisma_service_1.PrismaService, jwt_strategy_1.JwtStrategy, TourRoleGuard, jwt_auth_guard_1.JwtAuthGuard, admin_guard_1.AdminGuard, CommentGateway, core_2.Reflector, EmailService],
providers: [prisma_service_1.PrismaService, jwt_strategy_1.JwtStrategy, TourRoleGuard, jwt_auth_guard_1.JwtAuthGuard, admin_guard_1.AdminGuard, CommentGateway, core_2.Reflector, EmailService, config_1.ConfigService],
exports: [prisma_service_1.PrismaService]
})
], AppModule);
bootstrap().catch(err => {
if (err.message.includes('DATABASE_URL')) {
console.error('❌ Lỗi nghiêm trọng: Biến môi trường DATABASE_URL không được tải. Hãy chắc chắn rằng file .env tồn tại ở thư mục gốc của dự án và chứa giá trị này.');
}
console.error('💥 Lỗi khởi động Server:');
console.error(err);
process.exit(1);