BROWSER POLYFILL FIX
Comprehensive guidelines for AI-assisted development within the Editor platform
๐จ Browser Polyfill Fix: "process is not defined"
Fixing Node.js environment variables in Angular browser apps
๐ฅ The Problem
When trying to navigate to the Database page, you encountered:
โ Connection cancelled
๐งญ Navigating to: Database
ERROR ReferenceError: process is not defined
at mongodb-integration.service.ts:41:11
๐ฏ Root Cause
Angular v6+ removed Node.js polyfills, but our MongoDB and Redis services were trying to access process.env in the browser:
// โ This fails in browser:
apiKey: process.env['NX_MONGODB_API_KEY'] || 'fallback'
References:
โ The Fix
1. Added Browser Polyfills
Created apps/singularity/src/polyfills.ts:
// Polyfill for Node.js 'process' global in browser environment
(window as any).process = {
env: {
NODE_ENV: 'development',
DEBUG: undefined,
NX_MONGODB_API_KEY: '',
NX_REDIS_REST_URL: '',
NX_REDIS_API_KEY: ''
},
version: '',
platform: 'browser',
nextTick: (fn: Function) => setTimeout(fn, 0)
};
// Polyfill for Node.js 'global' variable
(window as any).global = window;
2. Updated Angular Configuration
Modified apps/singularity/project.json:
{
"polyfills": ["apps/singularity/src/polyfills.ts"]
}
3. Made Environment Access Safer
Updated both MongoDB and Redis services:
// โ
Safe environment variable access
function getEnvironmentVariable(key: string, fallback: string = ''): string {
const windowProcess = (window as any)?.process;
if (windowProcess?.env?.[key]) {
return windowProcess.env[key];
}
return fallback;
}
const MONGODB_CONFIG: MongoDBConfig = {
apiKey: getEnvironmentVariable('NX_MONGODB_API_KEY', 'development-api-key-placeholder'),
// ... other config
};
๐งช Testing the Fix
What You Should See Now
- Console Logs (Successful):
โ
Browser polyfills loaded - Node.js globals available
๐๏ธ Database Integration Component initializing...
๐ Checking browser polyfills...
- window.process: object
- window.global: object
- process.env available: true
- Navigation Works:
๐งญ Navigating to: Database
๐ LOCAL-FIRST DATABASE INITIALIZATION
๐ฆ 1. Initializing local IndexedDB database...
Testing Steps
- Restart Development Server:
nx serve singularity
Navigate to Database Page:
- Click "Database" in navigation
- Should load without "process is not defined" error
Check Console:
- Look for polyfill success messages
- No more ReferenceError messages
๐ง Local Development Focus
With the fix in place, the Database page now works and prioritizes local development:
๐ฅ PRIORITY 1: IndexedDB (Local Development) โ WORKING
๐ฅ PRIORITY 2: Firebase Firestore (Real-time) โ WORKING
๐ฅ PRIORITY 3: MongoDB Atlas (Optional Production) โ SAFE FALLBACK
๐ง PRIORITY 4: Redis Cache (Optional Performance) โ SAFE FALLBACK
Environment Variables for Production
When ready to use real cloud databases, add to your environment:
# .env file or environment variables:
NX_MONGODB_API_KEY=your_real_mongodb_api_key
NX_REDIS_REST_URL=https://your-redis-instance.upstash.io
NX_REDIS_API_KEY=your_real_redis_api_key
๐ Files Modified
apps/singularity/src/polyfills.ts- New browser polyfillsapps/singularity/project.json- Updated polyfills configurationmongodb-integration.service.ts- Safe environment variable accessredis-integration.service.ts- Safe environment variable accessdatabase-integration.component.ts- Added polyfill debugging
๐ Result
โ
Database page loads successfully
โ
No more "process is not defined" errors
โ
Local-first development approach working
โ
Cloud databases have safe fallbacks
โ
Ready for production scaling when needed
๐ Your app should now navigate to the Database page without any errors!