6.2 KiB
🔧 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 :
developmentproductiontest
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) :
environment:
NODE_ENV: production # ← Changé de "preprod" à "production"
PORT: "4000"
# ...
Frontend (ligne 157) :
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 :
NODE_ENV: production
Option 2 : Modifier la Validation Backend (Alternative)
Si vous voulez vraiment utiliser preprod, modifier apps/backend/src/app.module.ts :
// 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(pasdebug) - 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
- Portainer → Stacks → xpeditis-preprod
- Editor → Copier le nouveau
portainer-stack.yml(avecNODE_ENV=production) - ✅ Cocher "Re-pull image and redeploy"
- 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
# 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
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)
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
# 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