Skip to content

Instantly share code, notes, and snippets.

@CiprianSpiridon
Created August 5, 2025 16:32
Show Gist options
  • Save CiprianSpiridon/be14d63859d19a19b01697fbba113b2c to your computer and use it in GitHub Desktop.
Save CiprianSpiridon/be14d63859d19a19b01697fbba113b2c to your computer and use it in GitHub Desktop.
Creates a Vercel-compatible environment file from the web app's .env.local file
#!/usr/bin/env node
/**
* Creates a Vercel-compatible environment file from the web app's .env.local file
*
* This script converts the generated .env.local file into a format that can be
* imported directly into Vercel's environment variables dashboard.
*
* Key differences from .env.local format:
* - Removes quotes around values (Vercel adds them automatically)
* - Strips comments and empty lines
* - Maintains all variable names and values exactly as they are
*
* Usage:
* 1. First run: node scripts/sync-env.js --env prod
* 2. Then run: node scripts/create-vercel-env.js
* 3. Upload the generated vercel.env file to Vercel Dashboard
*/
const fs = require('fs')
const path = require('path')
try {
// Read the web app's environment file (should be generated with production values)
const envPath = path.join(__dirname, '../apps/web/.env.local')
const envContent = fs.readFileSync(envPath, 'utf8')
// Parse environment variables, excluding comments and empty lines
// This ensures we only get actual variable definitions
const lines = envContent
.split('\n')
.filter(line => line.trim() && !line.startsWith('#'))
// Format for Vercel: remove quotes around values
// Vercel expects: KEY=value (not KEY="value")
const vercelEnv = lines
.map(line => {
// Split on first '=' to handle values that contain '=' characters
const [key, ...valueParts] = line.split('=')
const value = valueParts.join('=').replace(/^"/, '').replace(/"$/, '')
return `${key}=${value}`
})
.join('\n')
// Write the Vercel-compatible environment file to project root
const outputPath = path.join(__dirname, '../vercel.env')
fs.writeFileSync(outputPath, vercelEnv)
console.log(`✅ Created vercel.env with ${lines.length} environment variables`)
console.log('📋 You can now upload this file to Vercel:')
console.log(' 1. Go to your Vercel project settings')
console.log(' 2. Navigate to Environment Variables')
console.log(' 3. Click "Import .env File"')
console.log(' 4. Upload the vercel.env file')
console.log(' 5. Deploy your application')
// Show a preview of what variables are included (names only, not values)
console.log('\n📋 Preview of environment variables:')
lines.slice(0, 5).forEach(line => {
const [key] = line.split('=')
console.log(` • ${key}`)
})
if (lines.length > 5) {
console.log(` ... and ${lines.length - 5} more`)
}
// Important reminder about environment selection
console.log('\n⚠️ Make sure you ran "node scripts/sync-env.js --env prod" first!')
console.log(' This ensures production URLs (https://www.shiptoday.dev) are used.')
} catch (error) {
console.error('❌ Error creating Vercel environment file:', error.message)
console.error('💡 Make sure to run "node scripts/sync-env.js --env prod" first')
process.exit(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment