# 🔧 Fix NODE_ENV pour Portainer ## 🚹 ProblĂšme IdentifiĂ© ### Backend Erreur ``` ERROR [ExceptionHandler] Config validation error: "NODE_ENV" must be one of [development, production, test] ``` **Cause** : La validation Joi dans `apps/backend/src/app.module.ts` (ligne 35) n'accepte QUE : - `development` - `production` - `test` Mais le stack utilisait `NODE_ENV=preprod` ❌ ### Frontend RedĂ©marrage en Boucle Le frontend redĂ©marrait sans cesse (status "complete" rĂ©pĂ©tĂ©) parce que le backend crashait, donc le health check Ă©chouait. --- ## ✅ Solution AppliquĂ©e ### Changement dans `docker/portainer-stack.yml` **Backend (ligne 83)** : ```yaml environment: NODE_ENV: production # ← ChangĂ© de "preprod" Ă  "production" PORT: "4000" # ... ``` **Frontend (ligne 157)** : ```yaml environment: NODE_ENV: production # ← ChangĂ© de "preprod" Ă  "production" NEXT_PUBLIC_API_URL: https://api.preprod.xpeditis.com # ... ``` --- ## 🎯 Pourquoi `production` et Pas `preprod` ? ### Option 1 : Utiliser `production` (✅ SOLUTION ACTUELLE) **Avantages** : - ✅ Fonctionne immĂ©diatement sans changer le code - ✅ Active les optimisations de production (logs niveau info, pas de debug) - ✅ Comportement attendu pour un environnement de pre-production **Configuration** : ```yaml NODE_ENV: production ``` ### Option 2 : Modifier la Validation Backend (Alternative) Si vous voulez vraiment utiliser `preprod`, modifier `apps/backend/src/app.module.ts` : ```typescript // Ligne 35 NODE_ENV: Joi.string() .valid('development', 'production', 'test', 'preprod') // ← Ajouter 'preprod' .default('development'), ``` **InconvĂ©nients** : - ❌ NĂ©cessite rebuild des images Docker - ❌ Re-trigger la CI/CD - ❌ Pas standard (Node.js attend development/production/test) --- ## 📊 Impact du NODE_ENV ### Backend (NestJS) **NODE_ENV=production** active : - Logs niveau `info` (pas `debug`) - Logging optimisĂ© (JSON, pas pino-pretty) - Optimisations de performance - Caching agressif **NODE_ENV=development** active : - Logs niveau `debug` (verbose) - Pino-pretty avec couleurs (plus lisible mais plus lent) - Pas de caching - Hot reload (non applicable en Docker) ### Frontend (Next.js) **NODE_ENV=production** active : - Build optimisĂ© (minification, tree-shaking) - Images optimisĂ©es - Pas de React DevTools - Meilleure performance --- ## 🔍 VĂ©rification Post-Fix ### Backend : Logs Attendus **Portainer → Containers → xpeditis-backend → Logs** : ``` 🚀 Starting Xpeditis Backend... ⏳ Waiting for PostgreSQL to be ready... ✅ PostgreSQL is ready 🔄 Running database migrations... ✅ DataSource initialized ✅ Successfully ran 10 migration(s) ✅ Database migrations completed 🚀 Starting NestJS application... [Nest] 1 - LOG [NestFactory] Starting Nest application... [Nest] 1 - LOG [InstanceLoader] AppModule dependencies initialized [Nest] 1 - LOG [RoutesResolver] AppController {/api/v1}: [Nest] 1 - LOG Application is running on: http://0.0.0.0:4000 ``` **Plus d'erreur de validation ✅** ### Frontend : Logs Attendus **Portainer → Containers → xpeditis-frontend → Logs** : ``` â–Č Next.js 14.0.4 - Local: http://localhost:3000 - Network: http://0.0.0.0:3000 ✓ Ready in 88ms ``` **Container reste en Ă©tat "running" (pas de redĂ©marrage) ✅** --- ## 📋 Checklist de DĂ©ploiement (Mise Ă  Jour) ### 1. Update le Stack Portainer 1. **Portainer → Stacks → xpeditis-preprod** 2. **Editor** → Copier le nouveau `portainer-stack.yml` (avec `NODE_ENV=production`) 3. ✅ **Cocher "Re-pull image and redeploy"** 4. **Update the stack** ### 2. VĂ©rifier les Services **Portainer → Containers** : | Container | État Attendu | Logs ClĂ©s | |-----------|--------------|-----------| | `xpeditis-backend` | Running (Healthy) | `✅ Database migrations completed` | | `xpeditis-frontend` | Running (Healthy) | `✓ Ready in XXms` | ### 3. Tester les Endpoints ```bash # Backend health check curl https://api.preprod.xpeditis.com/api/v1/health # RĂ©ponse : {"status":"ok","info":{"database":{"status":"up"},"redis":{"status":"up"}}} # Frontend curl -I https://app.preprod.xpeditis.com # RĂ©ponse : HTTP/2 200 ``` --- ## 🎯 RĂ©sumĂ© du Fix | Avant | AprĂšs | RĂ©sultat | |-------|-------|----------| | `NODE_ENV: preprod` | `NODE_ENV: production` | ✅ Backend dĂ©marre | | Backend crash | Backend running | ✅ Migrations OK | | Frontend loop | Frontend stable | ✅ Reste en "running" | --- ## 🔧 Si Vous Voulez Vraiment Utiliser `preprod` ### Étape 1 : Modifier le Backend **Fichier** : `apps/backend/src/app.module.ts` ```typescript validationSchema: Joi.object({ NODE_ENV: Joi.string() .valid('development', 'production', 'test', 'preprod') // ← Ajouter .default('development'), // ... }), ``` ### Étape 2 : Ajuster les Conditions **Fichier** : `apps/backend/src/app.module.ts` (ligne 56-66) ```typescript LoggerModule.forRootAsync({ useFactory: (configService: ConfigService) => { const env = configService.get('NODE_ENV'); const isDev = env === 'development'; const isProd = env === 'production' || env === 'preprod'; // ← Traiter preprod comme prod return { pinoHttp: { transport: isDev ? { /* ... */ } : undefined, level: isProd ? 'info' : 'debug', }, }; }, }), ``` ### Étape 3 : Rebuild et Push ```bash # Commit changes git add apps/backend/src/app.module.ts git commit -m "feat: add preprod to NODE_ENV validation" # Push to trigger CI/CD git push origin preprod # Attendre que CI/CD rebuild et push les images (~15 min) ``` ### Étape 4 : Update Stack Portainer Changer `NODE_ENV: production` → `NODE_ENV: preprod` dans le stack. --- **Recommandation** : **Garder `NODE_ENV=production`** car : - ✅ Standard Node.js/NestJS - ✅ Fonctionne immĂ©diatement - ✅ Pre-production = production-like environment --- **Date** : 2025-11-19 **Fix AppliquĂ©** : `NODE_ENV=production` dans portainer-stack.yml **Status** : ✅ PrĂȘt pour dĂ©ploiement