# 🔧 CORREÇÃO: npm executado no diretório errado

## 🔴 PROBLEMA IDENTIFICADO

### **Erro:**
```
npm error path /var/www/gestorstream/package.json
npm error errno -2
npm error enoent Could not read package.json: Error: ENOENT: no such file or directory
```

### **Causa:**
O comando `npm install` e `npm run build` foram executados no diretório **raiz** (`/var/www/gestorstream/`), mas o `package.json` está no diretório **frontend** (`/var/www/gestorstream/frontend/`).

---

## ✅ SOLUÇÃO

### **Comandos Corretos:**

```bash
# 1. Entrar no diretório frontend
cd /var/www/gestorstream/frontend

# 2. Instalar dependências
sudo -u www-data npm install

# 3. Build do frontend
sudo -u www-data npm run build

# 4. Verificar se index.html foi criado
ls -lh dist/index.html
```

---

## 🚀 SCRIPT COMPLETO DE CORREÇÃO

Execute este script completo:

```bash
#!/bin/bash
# build-frontend-correto.sh

APP_DIR="/var/www/gestorstream"
FRONTEND_DIR="$APP_DIR/frontend"

echo "🔧 Build do Frontend - Diretório Correto"
echo ""

# Verificar se diretório existe
if [ ! -d "$FRONTEND_DIR" ]; then
    echo "❌ Erro: Diretório $FRONTEND_DIR não encontrado"
    exit 1
fi

cd "$FRONTEND_DIR" || exit 1

echo "📁 Diretório atual: $(pwd)"
echo ""

# Verificar se package.json existe
if [ ! -f "package.json" ]; then
    echo "❌ Erro: package.json não encontrado em $FRONTEND_DIR"
    exit 1
fi

echo "✅ package.json encontrado"
echo ""

# Ajustar permissões
echo "🔐 Ajustando permissões..."
sudo chown -R www-data:www-data "$FRONTEND_DIR"
sudo chmod -R 755 "$FRONTEND_DIR"
echo "✅ Permissões ajustadas"
echo ""

# Limpar dist/ se existir
if [ -d "dist" ]; then
    echo "🧹 Removendo dist/ antigo..."
    sudo rm -rf dist
    echo "✅ dist/ removido"
    echo ""
fi

# Instalar dependências
echo "📦 Instalando dependências..."
sudo -u www-data npm install --no-audit --no-fund

if [ $? -ne 0 ]; then
    echo "❌ Erro ao instalar dependências"
    exit 1
fi

echo "✅ Dependências instaladas"
echo ""

# Build
echo "🔨 Executando build..."
sudo -u www-data npm run build

if [ $? -ne 0 ]; then
    echo "❌ Erro no build"
    exit 1
fi

echo "✅ Build concluído"
echo ""

# Verificar index.html
if [ -f "dist/index.html" ]; then
    echo "✅ index.html criado com sucesso!"
    ls -lh dist/index.html
else
    echo "❌ ERRO: index.html não foi criado!"
    exit 1
fi

echo ""
echo "✅ Frontend buildado com sucesso!"
```

**Salvar e executar:**
```bash
nano build-frontend-correto.sh
# (cole o conteúdo acima)
chmod +x build-frontend-correto.sh
sudo bash build-frontend-correto.sh
```

---

## 📊 ESTRUTURA DO PROJETO

```
/var/www/gestorstream/
├── backend/              # Laravel (PHP)
│   ├── app/
│   ├── routes/
│   └── package.json      # ❌ NÃO EXISTE
├── frontend/             # React/Vite (Node.js)
│   ├── src/
│   ├── package.json      # ✅ AQUI ESTÁ!
│   ├── dist/             # ✅ Build output
│   └── node_modules/
└── package.json          # ❌ NÃO EXISTE
```

**Comandos npm devem ser executados em `frontend/`, não na raiz!**

---

## 🎯 COMANDOS RÁPIDOS

```bash
# Correto ✅
cd /var/www/gestorstream/frontend
sudo -u www-data npm install
sudo -u www-data npm run build

# Errado ❌
cd /var/www/gestorstream
sudo -u www-data npm install  # ❌ Não funciona!
```

---

## 🔍 VERIFICAÇÃO

Após executar os comandos corretos:

```bash
# Verificar se index.html existe
ls -lh /var/www/gestorstream/frontend/dist/index.html

# Deve mostrar algo como:
# -rw-r--r-- 1 www-data www-data 2.5K Jan 14 18:30 /var/www/gestorstream/frontend/dist/index.html
```

Se `index.html` existir, o build foi bem-sucedido! ✅
