Skip to main content

setup-backend.sh

Automates the complete backend setup process.

Purpose

Sets up the backend Node.js environment by installing dependencies, creating configuration files, and building the TypeScript source code.

Location

Must be run from the scripts/ directory:

cd scripts
./setup-backend.sh

What It Does

  1. Validates Node.js installation - Checks for Node.js 18+ and npm
  2. Installs dependencies - Runs npm install in the backend directory
  3. Creates .env file - Copies from .env.example or creates a template
  4. Builds TypeScript - Compiles source to backend/dist/

Prerequisites

  • Node.js 18.x or higher
  • npm 9.x or higher

Output

On success, displays:

✓ Node.js vX.X.X found
✓ npm vX.X.X found
✓ Dependencies installed successfully
✓ Created .env from .env.example
✓ Build successful

Backend Setup Complete!

Next Steps

After running this script:

  1. Edit backend/.env and add your OpenAI API key:

    OPENAI_API_KEY=sk-...your-key...
  2. (Optional) Set up Firebase:

    • Download serviceAccountKey.json from Firebase Console
    • Place in backend/ directory
  3. Start the backend:

    ./start-backend.sh
    # or
    ./start-dev.sh # starts both backend and frontend

Environment Variables

The script creates a .env file with these defaults:

VariableDefaultRequired
OPENAI_API_KEYyour_openai_api_key_hereYes
AI_PROVIDERopenaiYes
AI_MODELgpt-4-turbo-previewYes
PORT3000Yes
NODE_ENVdevelopmentYes
FIREBASE_SERVICE_ACCOUNT_PATHserviceAccountKey.jsonNo
ALLOWED_ORIGINShttp://localhost:*,http://127.0.0.1:*Yes
LOG_LEVELinfoYes

Troubleshooting

Node.js not found

# Install Node.js 18+
# Visit https://nodejs.org/

npm install fails

cd backend
npm cache clean --force
rm -rf node_modules package-lock.json
npm install

Build fails

cd backend
npm install -D typescript
npm run build

Source Code

Located at: scripts/setup-backend.sh

Key features:

  • Color-coded output (green=success, red=error, yellow=warning)
  • Automatic error exit (set -e)
  • Prerequisite validation
  • Idempotent (safe to re-run)