Local Development Setup - Tectonica-Campaigns-Solutions/Tectonica-Dev-Team-Standards-Practices GitHub Wiki

Development Environment Setup

Overview

This guide walks through setting up your local development environment for our Jamstack projects using Next.js, DatoCMS, and Netlify/Vercel. Following these steps ensures consistency across the team and prevents common setup issues.

Table of Contents

Prerequisites

Before starting, ensure you have the following installed:

  • Node.js: v18.17.0 or higher (check with node -v)
  • npm: v9.0.0 or higher (check with npm -v) OR yarn: v1.22.0 or higher
  • Git: Latest version
  • Code Editor: VS Code (recommended) with team extensions
  • DatoCMS Account: Access to the project workspace

Recommended Tools

Repository Setup

1. Clone the Repository

# Using SSH (recommended)
git clone [email protected]:your-org/project-name.git

# Using HTTPS
git clone https://github.com/your-org/project-name.git

# Navigate to project directory
cd project-name

2. Verify Branch

# Check current branch
git branch

# Switch to development branch
git checkout develop

Environment Variables Configuration

1. Create Local Environment File

# Copy the example environment file
cp .env.example .env.local

2. Required Environment Variables

Your .env.local file should include:

# DatoCMS Configuration
NEXT_PUBLIC_DATOCMS_API_TOKEN=your-read-only-api-token
DATOCMS_PREVIEW_SECRET=your-preview-secret
DATOCMS_API_TOKEN=your-full-access-token
NEXT_PUBLIC_DATOCMS_ENVIRONMENT=main

# Next.js Configuration
NEXT_PUBLIC_SITE_URL=http://localhost:3000

# Deployment Platform (if needed locally)
NETLIFY_SITE_ID=your-site-id
VERCEL_PROJECT_ID=your-project-id

# Additional Services (as needed)
NEXT_PUBLIC_GTM_ID=your-gtm-id
SMTP_HOST=your-smtp-host
SMTP_PORT=587

3. Obtaining API Keys

DatoCMS Tokens

  1. Log in to DatoCMS
  2. Navigate to your project
  3. Go to Settings → API Tokens
  4. Create/copy the following tokens:
    • Read-only API Token: For public content fetching
    • Full-access API Token: For preview mode and migrations
    • Preview Secret: Random string for securing preview routes

Other Services

Document where to obtain other API keys your project uses.

4. Environment Variables Security

⚠️ Important Security Notes:

  • Never commit .env.local to version control
  • .env.local should be in .gitignore
  • Use different tokens for development and production
  • Rotate tokens regularly
  • Store production secrets in Netlify/Vercel dashboard

Installing Dependencies

Using npm

# Install all dependencies
npm install

# Install with clean slate (if having issues)
rm -rf node_modules package-lock.json
npm install

Using yarn

# Install all dependencies
yarn install

# Install with clean slate (if having issues)
rm -rf node_modules yarn.lock
yarn install

Common Dependency Issues

If you encounter permission errors:

# Clear npm cache
npm cache clean --force

# Use node version specified in .nvmrc
nvm use

DatoCMS Connection

1. Verify DatoCMS Environment

Ensure you're connecting to the correct DatoCMS environment:

// lib/datocms.js - verify this configuration
const client = new GraphQLClient('https://graphql.datocms.com/', {
  headers: {
    authorization: `Bearer ${process.env.DATOCMS_API_TOKEN}`,
    'X-Environment': process.env.NEXT_PUBLIC_DATOCMS_ENVIRONMENT || 'main',
  },
});

2. Test DatoCMS Connection

Create a test script scripts/test-datocms.js:

require('dotenv').config({ path: '.env.local' });

async function testConnection() {
  try {
    const response = await fetch('https://graphql.datocms.com/', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${process.env.DATOCMS_API_TOKEN}`,
        'X-Environment': process.env.NEXT_PUBLIC_DATOCMS_ENVIRONMENT || 'main',
      },
      body: JSON.stringify({
        query: `{ _site { globalSeo { siteName } } }`
      }),
    });
    
    const data = await response.json();
    console.log('✅ DatoCMS Connection Successful:', data);
  } catch (error) {
    console.error('❌ DatoCMS Connection Failed:', error);
  }
}

testConnection();

Run with: node scripts/test-datocms.js

3. Setting Up Preview Mode

For Next.js preview mode with DatoCMS:

  1. Ensure preview API route exists at pages/api/preview.js or app/api/preview/route.js
  2. Configure preview URL in DatoCMS:
    • Go to Settings → Webhooks
    • Set preview URL: https://your-site.com/api/preview?secret=YOUR_SECRET&slug={slug}

Running Development Servers

1. Start Next.js Development Server

# Using npm
npm run dev

# Using yarn
yarn dev

# With specific port
npm run dev -- -p 3001

2. Available Scripts

Standard scripts in package.json:

{
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "test": "jest",
    "type-check": "tsc --noEmit"
  }
}

3. Development Server Features

Once running, you'll have access to:

Verification Steps

Run through these checks to ensure proper setup:

1. Build Test

npm run build

✅ Build completes without errors

2. Type Checking

npm run type-check

✅ No TypeScript errors

3. Linting

npm run lint

✅ No linting errors

4. DatoCMS Content

  • Navigate to a page that fetches DatoCMS content
  • ✅ Content loads properly
  • ✅ Images from DatoCMS display correctly

5. Preview Mode

  • Access /api/preview?secret=YOUR_SECRET&slug=/
  • ✅ Preview bar appears
  • ✅ Draft content is visible

Troubleshooting

Common Issues and Solutions

Port Already in Use

# Find process using port 3000
lsof -i :3000

# Kill the process
kill -9 <PID>

DatoCMS Connection Errors

  • Verify API token is correct and active
  • Check environment variable names match exactly
  • Ensure you're using the correct environment (main/development)

Module Not Found Errors

# Clear cache and reinstall
rm -rf node_modules .next
npm install
npm run dev

Environment Variables Not Loading

  • Ensure .env.local is in project root
  • Restart dev server after changing env vars
  • Check for typos in variable names

Getting Help

If you encounter issues not covered here:

  1. Check project-specific README
  2. Search team documentation
  3. Ask in #dev-help Slack channel
  4. Create a GitHub issue with details

Related Pages

Last Updated

June 9, 2025 - Initial setup guide created

⚠️ **GitHub.com Fallback** ⚠️