10 Error Paling Umum yang Dialami Developer dan Cara Fixnya
Tidak peduli seberapa berpengalaman seorang developer, ada sekumpulan error yang akan selalu ditemui berulang kali sepanjang karir. Artikel ini merangkum 10 error paling umum, lengkap dengan penyebab dan cara fix yang langsung bisa diterapkan.
1. Cannot read properties of undefined (reading 'x')
Bahasa: JavaScript / TypeScript
// Error:
// TypeError: Cannot read properties of undefined (reading 'name')
// Penyebab: mengakses property dari nilai yang undefined/null
const user = await getUser(id); // Bisa return undefined jika tidak ditemukan
console.log(user.name); // 💥 Error jika user = undefined
// Fix 1: Optional chaining (?.)
console.log(user?.name); // undefined, tidak error
// Fix 2: Null check eksplisit
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
console.log(user.name); // Aman
// Fix 3: Default value dengan nullish coalescing (??)
const name = user?.name ?? 'Anonymous';
// Fix 4: TypeScript strict null checks
// tsconfig.json: "strictNullChecks": true
// TypeScript akan memaksa kamu handle null/undefined
2. CORS Error
Bahasa: Semua (masalah di browser)
// Error:
// Access to fetch at 'http://localhost:3001/api' from origin
// 'http://localhost:3000' has been blocked by CORS policy
// Fix cepat di Express:
import cors from 'cors';
app.use(cors({ origin: 'http://localhost:3000' }));
// Fix untuk development (izinkan semua):
app.use(cors());
// Fix di Vite (proxy — tidak perlu ubah backend):
// vite.config.ts
server: {
proxy: {
'/api': { target: 'http://localhost:3001', changeOrigin: true }
}
}
3. Port Already in Use
Bahasa: Semua
# Error:
# Error: listen EADDRINUSE: address already in use :::3000
# Cari proses yang pakai port tersebut
lsof -i :3000
# Kill proses
kill -9 [PID]
# Atau satu perintah:
kill -9 $(lsof -t -i:3000)
# Windows:
netstat -ano | findstr :3000
taskkill /PID [PID] /F
# Fix permanen: tambahkan ke package.json
"scripts": {
"dev": "kill-port 3000 && node server.js"
}
# npm install kill-port
4. Git Merge Conflict
Tool: Git
# Error:
# CONFLICT (content): Merge conflict in src/app.ts
# Automatic merge failed; fix conflicts and then commit the result.
# Lihat semua file yang conflict
git status
# Resolve di VS Code (paling mudah):
# Buka file → klik "Accept Current/Incoming/Both Changes"
# Atau resolve manual: hapus marker >>>>>>
# Lalu:
git add [file yang sudah di-resolve]
git commit
# Batalkan merge jika terlalu rumit:
git merge --abort
5. Module Not Found / Cannot find module
Bahasa: JavaScript / TypeScript / Python
// Error:
// Error: Cannot find module './utils/helper'
// Module not found: Error: Can't resolve '@/components/Button'
// Fix 1: Cek path dan nama file (case-sensitive di Linux!)
// File asli: utils/Helper.ts → import dari './utils/helper' ❌
// Harus: import dari './utils/Helper' ✅
// Fix 2: Install dependency yang hilang
npm install [package-name]
// Fix 3: Konfigurasi path alias di tsconfig.json
{
"compilerOptions": {
"paths": {
"@/*": ["./src/*"]
}
}
}
// Fix 4: Hapus node_modules dan install ulang
rm -rf node_modules package-lock.json
npm install
6. JWT TokenExpiredError
Bahasa: Semua (backend)
// Error:
// JsonWebTokenError: TokenExpiredError: jwt expired
// Ini bukan bug — token memang sudah kadaluarsa (behavior yang benar)
// Yang perlu diperbaiki: cara handle-nya
// Fix di middleware:
try {
req.user = jwt.verify(token, process.env.JWT_SECRET);
next();
} catch (err) {
if (err.name === 'TokenExpiredError') {
return res.status(401).json({
error: 'Sesi berakhir, silakan login ulang',
code: 'TOKEN_EXPIRED' // Frontend bisa auto-refresh berdasarkan code ini
});
}
return res.status(401).json({ error: 'Token tidak valid' });
}
7. Environment Variable Undefined di Production
Bahasa: Semua
// Error:
// Error: DATABASE_URL is required
// TypeError: Cannot read properties of undefined (reading 'split')
// Penyebab: .env tidak di-deploy, atau env var tidak di-set di server
// Fix 1: Validasi env var saat startup
const required = ['DATABASE_URL', 'JWT_SECRET', 'PORT'];
const missing = required.filter(key => !process.env[key]);
if (missing.length > 0) {
console.error('Missing env vars:', missing.join(', '));
process.exit(1);
}
// Fix 2: Pastikan dotenv di-load PERTAMA
import 'dotenv/config'; // Harus baris pertama!
import express from 'express';
// ...
// Fix 3: Set env var di server/CI/CD
# Vercel: Settings → Environment Variables
# GitHub Actions: Settings → Secrets
# VPS: export DATABASE_URL=... di ~/.bashrc
8. Docker Container Langsung Exit (Exit Code 1)
Tool: Docker
# Error:
# Exited (1) 5 seconds ago
# Cek log untuk tahu penyebabnya
docker logs [container-name]
# Penyebab umum:
# 1. Env var tidak ada → tambahkan --env-file .env
docker run --env-file .env myapp
# 2. Port sudah dipakai → ganti port mapping
docker run -p 3001:3000 myapp
# 3. File tidak ditemukan → cek WORKDIR dan COPY di Dockerfile
# Debug interaktif:
docker run -it --entrypoint /bin/sh myapp
9. Nginx 502 Bad Gateway
Tool: Nginx
# Error: 502 Bad Gateway
# Diagnosis cepat:
sudo tail -f /var/log/nginx/error.log
# Penyebab paling umum: backend tidak jalan
pm2 status
pm2 restart myapp
# Test backend langsung (bypass Nginx):
curl http://localhost:3000/health
# Jika backend jalan tapi masih 502, cek konfigurasi Nginx:
sudo nginx -t # Validasi syntax
# Pastikan port di proxy_pass sama dengan port yang dipakai aplikasi
10. Build Gagal di CI/CD tapi Lokal Jalan
Tool: GitHub Actions / GitLab CI / dll
# Penyebab paling umum:
# 1. Versi Node.js berbeda → pin versi di CI
- uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
# 2. npm install vs npm ci
# Selalu gunakan npm ci di CI/CD!
- run: npm ci
# 3. Env var tidak di-set di CI
# Tambahkan di Settings → Secrets
# 4. Case sensitivity (macOS case-insensitive, Linux case-sensitive)
# import './userprofile' ≠ import './UserProfile' di Linux
# Debug: jalankan CI di lokal
brew install act
act push
Bonus: Cara Cepat Debug Error Apapun
Ketika menemukan error yang tidak familiar, ikuti langkah ini:
- Baca error message dengan teliti — terutama baris pertama dan stack trace teratas
- Copy error message ke Google — tambahkan nama framework/library untuk hasil yang lebih relevan
-
Cek apakah ada perubahan terbaru —
git diffuntuk lihat apa yang berubah - Isolasi masalah — buat reproduksi minimal yang menunjukkan error
- Tanya AI dengan konteks lengkap — paste error + kode + apa yang sudah dicoba
// Template tanya AI untuk debug:
"Stack: [Node.js 20 + Express + TypeScript]
Error:
[paste error message dan stack trace]
Kode yang menyebabkan error:
[paste kode]
Yang sudah aku coba:
- [langkah 1]
- [langkah 2]
Tolong analisis root cause dan berikan fix."
Kesimpulan
Semua error di atas punya pola yang sama: begitu kamu tahu penyebabnya, fix-nya relatif mudah. Yang membuat debugging terasa sulit adalah tidak tahu harus mulai dari mana. Simpan artikel ini sebagai referensi — dan lain kali ketika menemukan salah satu error ini, kamu sudah tahu langkah pertama yang harus diambil.
Artikel ini pertama kali diterbitkan di SavefileArchive.
Top comments (0)