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
- Validates Node.js installation - Checks for Node.js 18+ and npm
- Installs dependencies - Runs
npm installin the backend directory - Creates .env file - Copies from
.env.exampleor creates a template - 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:
-
Edit
backend/.envand add your OpenAI API key:OPENAI_API_KEY=sk-...your-key... -
(Optional) Set up Firebase:
- Download
serviceAccountKey.jsonfrom Firebase Console - Place in
backend/directory
- Download
-
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:
| Variable | Default | Required |
|---|---|---|
OPENAI_API_KEY | your_openai_api_key_here | Yes |
AI_PROVIDER | openai | Yes |
AI_MODEL | gpt-4-turbo-preview | Yes |
PORT | 3000 | Yes |
NODE_ENV | development | Yes |
FIREBASE_SERVICE_ACCOUNT_PATH | serviceAccountKey.json | No |
ALLOWED_ORIGINS | http://localhost:*,http://127.0.0.1:* | Yes |
LOG_LEVEL | info | Yes |
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)