claude-mpm 4.17.0__py3-none-any.whl → 4.18.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of claude-mpm might be problematic. Click here for more details.
- claude_mpm/VERSION +1 -1
- claude_mpm/agents/BASE_PM.md +48 -17
- claude_mpm/agents/agent_loader.py +4 -4
- claude_mpm/agents/templates/svelte-engineer.json +225 -0
- claude_mpm/config/agent_config.py +2 -2
- claude_mpm/core/factories.py +1 -1
- claude_mpm/core/optimized_agent_loader.py +3 -3
- claude_mpm/hooks/claude_hooks/response_tracking.py +35 -1
- claude_mpm/models/resume_log.py +340 -0
- claude_mpm/services/agents/auto_config_manager.py +1 -1
- claude_mpm/services/agents/deployment/agent_configuration_manager.py +1 -1
- claude_mpm/services/agents/deployment/agent_record_service.py +1 -1
- claude_mpm/services/agents/deployment/async_agent_deployment.py +1 -1
- claude_mpm/services/agents/deployment/local_template_deployment.py +1 -1
- claude_mpm/services/agents/local_template_manager.py +1 -1
- claude_mpm/services/core/path_resolver.py +1 -1
- claude_mpm/services/infrastructure/resume_log_generator.py +439 -0
- claude_mpm/services/mcp_config_manager.py +2 -2
- claude_mpm/services/session_manager.py +205 -1
- claude_mpm/services/unified/deployment_strategies/local.py +1 -1
- claude_mpm/skills/bundled/api-documentation.md +393 -0
- claude_mpm/skills/bundled/async-testing.md +571 -0
- claude_mpm/skills/bundled/code-review.md +143 -0
- claude_mpm/skills/bundled/database-migration.md +199 -0
- claude_mpm/skills/bundled/docker-containerization.md +194 -0
- claude_mpm/skills/bundled/express-local-dev.md +1429 -0
- claude_mpm/skills/bundled/fastapi-local-dev.md +1199 -0
- claude_mpm/skills/bundled/git-workflow.md +414 -0
- claude_mpm/skills/bundled/imagemagick.md +204 -0
- claude_mpm/skills/bundled/json-data-handling.md +223 -0
- claude_mpm/skills/bundled/nextjs-local-dev.md +807 -0
- claude_mpm/skills/bundled/pdf.md +141 -0
- claude_mpm/skills/bundled/performance-profiling.md +567 -0
- claude_mpm/skills/bundled/refactoring-patterns.md +180 -0
- claude_mpm/skills/bundled/security-scanning.md +327 -0
- claude_mpm/skills/bundled/systematic-debugging.md +473 -0
- claude_mpm/skills/bundled/test-driven-development.md +378 -0
- claude_mpm/skills/bundled/vite-local-dev.md +1061 -0
- claude_mpm/skills/bundled/web-performance-optimization.md +2305 -0
- claude_mpm/skills/bundled/xlsx.md +157 -0
- claude_mpm/utils/agent_dependency_loader.py +2 -2
- {claude_mpm-4.17.0.dist-info → claude_mpm-4.18.0.dist-info}/METADATA +68 -1
- {claude_mpm-4.17.0.dist-info → claude_mpm-4.18.0.dist-info}/RECORD +47 -24
- {claude_mpm-4.17.0.dist-info → claude_mpm-4.18.0.dist-info}/WHEEL +0 -0
- {claude_mpm-4.17.0.dist-info → claude_mpm-4.18.0.dist-info}/entry_points.txt +0 -0
- {claude_mpm-4.17.0.dist-info → claude_mpm-4.18.0.dist-info}/licenses/LICENSE +0 -0
- {claude_mpm-4.17.0.dist-info → claude_mpm-4.18.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,807 @@
|
|
|
1
|
+
---
|
|
2
|
+
skill_id: nextjs-local-dev
|
|
3
|
+
skill_version: 0.1.0
|
|
4
|
+
description: Managing Next.js development servers effectively, including direct dev server usage, PM2 for production deployments, and troubleshooting.
|
|
5
|
+
updated_at: 2025-10-30T17:00:00Z
|
|
6
|
+
tags: [nextjs, react, development, server]
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# Next.js Local Development Server
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
Next.js is a React framework for building full-stack web applications with built-in optimizations, routing, and rendering strategies. This skill focuses on managing Next.js development servers effectively, including direct dev server usage, PM2 for production deployments, and troubleshooting common development issues.
|
|
14
|
+
|
|
15
|
+
## When to Use This Skill
|
|
16
|
+
|
|
17
|
+
- Setting up Next.js development environment
|
|
18
|
+
- Configuring Turbopack for faster development
|
|
19
|
+
- Troubleshooting server startup and HMR issues
|
|
20
|
+
- Configuring PM2 for Next.js production deployments
|
|
21
|
+
- Resolving port conflicts and process management
|
|
22
|
+
- Optimizing development server performance
|
|
23
|
+
- Managing environment variables and configuration
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
### Development Server
|
|
28
|
+
|
|
29
|
+
**Standard Development (Node.js):**
|
|
30
|
+
```bash
|
|
31
|
+
npm run dev
|
|
32
|
+
# or
|
|
33
|
+
yarn dev
|
|
34
|
+
# or
|
|
35
|
+
pnpm dev
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
**With Turbopack (Next.js 13+):**
|
|
39
|
+
```bash
|
|
40
|
+
npm run dev -- --turbo
|
|
41
|
+
# or add to package.json
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
**Custom Port:**
|
|
45
|
+
```bash
|
|
46
|
+
npm run dev -- -p 3001
|
|
47
|
+
# or
|
|
48
|
+
PORT=3001 npm run dev
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### With PM2 (Production Only)
|
|
52
|
+
|
|
53
|
+
**Important:** PM2 should ONLY be used for production builds, NOT development servers. The dev server needs direct process control for HMR and file watching.
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
# Build first
|
|
57
|
+
npm run build
|
|
58
|
+
|
|
59
|
+
# Start with PM2
|
|
60
|
+
pm2 start npm --name "nextjs-app" -- start
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### With Docker
|
|
64
|
+
|
|
65
|
+
```dockerfile
|
|
66
|
+
# Development Dockerfile
|
|
67
|
+
FROM node:18-alpine
|
|
68
|
+
|
|
69
|
+
WORKDIR /app
|
|
70
|
+
|
|
71
|
+
COPY package*.json ./
|
|
72
|
+
RUN npm install
|
|
73
|
+
|
|
74
|
+
COPY . .
|
|
75
|
+
|
|
76
|
+
EXPOSE 3000
|
|
77
|
+
|
|
78
|
+
CMD ["npm", "run", "dev"]
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**Docker Compose:**
|
|
82
|
+
```yaml
|
|
83
|
+
version: '3.8'
|
|
84
|
+
services:
|
|
85
|
+
nextjs:
|
|
86
|
+
build:
|
|
87
|
+
context: .
|
|
88
|
+
dockerfile: Dockerfile
|
|
89
|
+
ports:
|
|
90
|
+
- "3000:3000"
|
|
91
|
+
volumes:
|
|
92
|
+
- .:/app
|
|
93
|
+
- /app/node_modules
|
|
94
|
+
- /app/.next
|
|
95
|
+
environment:
|
|
96
|
+
- NODE_ENV=development
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Configuration Patterns
|
|
100
|
+
|
|
101
|
+
### Recommended PM2 Config (Production Only)
|
|
102
|
+
|
|
103
|
+
```javascript
|
|
104
|
+
// ecosystem.config.js
|
|
105
|
+
module.exports = {
|
|
106
|
+
apps: [{
|
|
107
|
+
name: 'nextjs-prod',
|
|
108
|
+
script: 'npm',
|
|
109
|
+
args: 'start',
|
|
110
|
+
instances: 'max',
|
|
111
|
+
exec_mode: 'cluster',
|
|
112
|
+
env: {
|
|
113
|
+
NODE_ENV: 'production',
|
|
114
|
+
PORT: 3000
|
|
115
|
+
},
|
|
116
|
+
error_file: './logs/pm2-error.log',
|
|
117
|
+
out_file: './logs/pm2-out.log',
|
|
118
|
+
log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
|
|
119
|
+
merge_logs: true,
|
|
120
|
+
autorestart: true,
|
|
121
|
+
max_memory_restart: '1G',
|
|
122
|
+
// NO watch mode for production
|
|
123
|
+
watch: false
|
|
124
|
+
}]
|
|
125
|
+
};
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Port Management
|
|
129
|
+
|
|
130
|
+
**Method 1: CLI Flag (Highest Priority)**
|
|
131
|
+
```bash
|
|
132
|
+
npm run dev -- -p 3001
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
**Method 2: Environment Variable**
|
|
136
|
+
```bash
|
|
137
|
+
# .env.local
|
|
138
|
+
PORT=3001
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
# Command line
|
|
143
|
+
PORT=3001 npm run dev
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
**Method 3: Package.json Script**
|
|
147
|
+
```json
|
|
148
|
+
{
|
|
149
|
+
"scripts": {
|
|
150
|
+
"dev": "next dev -p 3001",
|
|
151
|
+
"dev:turbo": "next dev --turbo -p 3001"
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
**Method 4: Next.js Config**
|
|
157
|
+
```javascript
|
|
158
|
+
// next.config.js
|
|
159
|
+
module.exports = {
|
|
160
|
+
// Note: Port must still be set via CLI or env var
|
|
161
|
+
// This config doesn't control dev server port
|
|
162
|
+
serverRuntimeConfig: {
|
|
163
|
+
port: process.env.PORT || 3000
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Environment Variables
|
|
169
|
+
|
|
170
|
+
**Development Variables (.env.local):**
|
|
171
|
+
```bash
|
|
172
|
+
# Server Configuration
|
|
173
|
+
PORT=3000
|
|
174
|
+
NODE_ENV=development
|
|
175
|
+
|
|
176
|
+
# Turbopack Configuration
|
|
177
|
+
NEXT_TURBOPACK_TRACING=1
|
|
178
|
+
|
|
179
|
+
# API Configuration
|
|
180
|
+
NEXT_PUBLIC_API_URL=http://localhost:4000
|
|
181
|
+
API_SECRET_KEY=your-secret-key
|
|
182
|
+
|
|
183
|
+
# Database
|
|
184
|
+
DATABASE_URL=postgresql://localhost:5432/mydb
|
|
185
|
+
|
|
186
|
+
# Feature Flags
|
|
187
|
+
NEXT_PUBLIC_ENABLE_FEATURE_X=true
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
**Load Order (Highest to Lowest Priority):**
|
|
191
|
+
1. `.env.local` (all environments, ignored by git)
|
|
192
|
+
2. `.env.development` or `.env.production` (environment-specific)
|
|
193
|
+
3. `.env` (all environments, checked into git)
|
|
194
|
+
|
|
195
|
+
**Public vs Private Variables:**
|
|
196
|
+
- Prefix with `NEXT_PUBLIC_` for browser access
|
|
197
|
+
- No prefix = server-side only
|
|
198
|
+
- Never expose secrets with `NEXT_PUBLIC_` prefix
|
|
199
|
+
|
|
200
|
+
## Framework-Specific Best Practices
|
|
201
|
+
|
|
202
|
+
### Turbopack Configuration (Next.js 13+)
|
|
203
|
+
|
|
204
|
+
**Enable Turbopack:**
|
|
205
|
+
```json
|
|
206
|
+
{
|
|
207
|
+
"scripts": {
|
|
208
|
+
"dev": "next dev --turbo"
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
**Performance Tracing:**
|
|
214
|
+
```bash
|
|
215
|
+
# Enable tracing for debugging
|
|
216
|
+
NEXT_TURBOPACK_TRACING=1 npm run dev -- --turbo
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
**Benefits:**
|
|
220
|
+
- Up to 700x faster updates than Webpack
|
|
221
|
+
- Incremental compilation
|
|
222
|
+
- Better memory efficiency
|
|
223
|
+
- Faster cold starts
|
|
224
|
+
|
|
225
|
+
**Current Status (2024):**
|
|
226
|
+
- Stable for development use
|
|
227
|
+
- Most features supported
|
|
228
|
+
- Some plugins may not work yet
|
|
229
|
+
- Check compatibility: https://nextjs.org/docs/app/api-reference/next-config-js/turbo
|
|
230
|
+
|
|
231
|
+
### Hot Module Replacement (HMR)
|
|
232
|
+
|
|
233
|
+
**HMR for Server Components:**
|
|
234
|
+
|
|
235
|
+
Next.js caches server component renders. Clear cache on changes:
|
|
236
|
+
|
|
237
|
+
```javascript
|
|
238
|
+
// app/components/ServerComponent.js
|
|
239
|
+
export const dynamic = 'force-dynamic'; // Disable caching
|
|
240
|
+
|
|
241
|
+
export default function ServerComponent() {
|
|
242
|
+
return <div>Content</div>;
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
**HMR for Client Components:**
|
|
247
|
+
|
|
248
|
+
```javascript
|
|
249
|
+
'use client';
|
|
250
|
+
|
|
251
|
+
export default function ClientComponent() {
|
|
252
|
+
// HMR works automatically
|
|
253
|
+
return <div>Content</div>;
|
|
254
|
+
}
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
**HMR Not Working? Check:**
|
|
258
|
+
1. File is in `app/` or `pages/` directory
|
|
259
|
+
2. No syntax errors in file
|
|
260
|
+
3. WebSocket connection established (check Network tab)
|
|
261
|
+
4. Not running through PM2 (breaks HMR)
|
|
262
|
+
5. Docker volume mounts correct
|
|
263
|
+
|
|
264
|
+
### Server Actions and API Routes
|
|
265
|
+
|
|
266
|
+
**Server Actions (App Router):**
|
|
267
|
+
```javascript
|
|
268
|
+
// app/actions.js
|
|
269
|
+
'use server';
|
|
270
|
+
|
|
271
|
+
export async function createUser(formData) {
|
|
272
|
+
const name = formData.get('name');
|
|
273
|
+
// Database operations
|
|
274
|
+
return { success: true };
|
|
275
|
+
}
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
**API Routes (App Router):**
|
|
279
|
+
```javascript
|
|
280
|
+
// app/api/users/route.js
|
|
281
|
+
export async function GET(request) {
|
|
282
|
+
return Response.json({ users: [] });
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export async function POST(request) {
|
|
286
|
+
const body = await request.json();
|
|
287
|
+
return Response.json({ success: true });
|
|
288
|
+
}
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
**Development Server Auto-Reload:**
|
|
292
|
+
- Changes to API routes trigger automatic reload
|
|
293
|
+
- Changes to server actions trigger recompilation
|
|
294
|
+
- Client-side code uses HMR without full reload
|
|
295
|
+
|
|
296
|
+
### Memory Management
|
|
297
|
+
|
|
298
|
+
**Clear Build Cache:**
|
|
299
|
+
```bash
|
|
300
|
+
rm -rf .next
|
|
301
|
+
npm run dev
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
**Monitor Memory Usage:**
|
|
305
|
+
```bash
|
|
306
|
+
# Check Node.js process memory
|
|
307
|
+
ps aux | grep next
|
|
308
|
+
|
|
309
|
+
# Set max old space size
|
|
310
|
+
NODE_OPTIONS='--max-old-space-size=4096' npm run dev
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
**Optimize for Large Projects:**
|
|
314
|
+
```javascript
|
|
315
|
+
// next.config.js
|
|
316
|
+
module.exports = {
|
|
317
|
+
experimental: {
|
|
318
|
+
// Reduce memory usage
|
|
319
|
+
optimizeCss: true,
|
|
320
|
+
optimizePackageImports: ['lodash', 'react-icons']
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
## Common Problems & Solutions
|
|
326
|
+
|
|
327
|
+
### Problem 1: Port Already in Use (EADDRINUSE)
|
|
328
|
+
|
|
329
|
+
**Symptoms:**
|
|
330
|
+
```
|
|
331
|
+
Error: listen EADDRINUSE: address already in use :::3000
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
**Root Cause:**
|
|
335
|
+
Another process is using port 3000 (previous Next.js instance, other server, or zombie process).
|
|
336
|
+
|
|
337
|
+
**Solution:**
|
|
338
|
+
|
|
339
|
+
**Option A: Find and Kill Process**
|
|
340
|
+
```bash
|
|
341
|
+
# macOS/Linux
|
|
342
|
+
lsof -ti:3000 | xargs kill -9
|
|
343
|
+
|
|
344
|
+
# Windows
|
|
345
|
+
netstat -ano | findstr :3000
|
|
346
|
+
taskkill /PID <PID> /F
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
**Option B: Use Different Port**
|
|
350
|
+
```bash
|
|
351
|
+
npm run dev -- -p 3001
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
**Option C: Graceful Cleanup Script**
|
|
355
|
+
```json
|
|
356
|
+
{
|
|
357
|
+
"scripts": {
|
|
358
|
+
"dev:clean": "kill-port 3000 && npm run dev",
|
|
359
|
+
"predev": "kill-port 3000 || true"
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
Install `kill-port`: `npm install -D kill-port`
|
|
365
|
+
|
|
366
|
+
### Problem 2: Changes Not Reflecting (HMR Broken)
|
|
367
|
+
|
|
368
|
+
**Symptoms:**
|
|
369
|
+
- Code changes don't appear in browser
|
|
370
|
+
- Need to manually refresh or restart server
|
|
371
|
+
- Console shows "WebSocket connection failed"
|
|
372
|
+
|
|
373
|
+
**Root Cause:**
|
|
374
|
+
Multiple possible causes: PM2 interference, Docker volume issues, WebSocket proxy problems, or caching.
|
|
375
|
+
|
|
376
|
+
**Solution:**
|
|
377
|
+
|
|
378
|
+
**Step 1: Verify Direct Dev Server**
|
|
379
|
+
```bash
|
|
380
|
+
# Stop PM2 if running
|
|
381
|
+
pm2 stop all
|
|
382
|
+
|
|
383
|
+
# Run dev server directly
|
|
384
|
+
npm run dev
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
**Step 2: Clear All Caches**
|
|
388
|
+
```bash
|
|
389
|
+
rm -rf .next
|
|
390
|
+
rm -rf node_modules/.cache
|
|
391
|
+
npm run dev
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
**Step 3: Check WebSocket Connection**
|
|
395
|
+
|
|
396
|
+
In browser console, verify:
|
|
397
|
+
```
|
|
398
|
+
[HMR] connected
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
If not connected, check next.config.js:
|
|
402
|
+
```javascript
|
|
403
|
+
module.exports = {
|
|
404
|
+
// For proxy/reverse proxy setups
|
|
405
|
+
assetPrefix: process.env.ASSET_PREFIX || '',
|
|
406
|
+
|
|
407
|
+
// WebSocket configuration
|
|
408
|
+
experimental: {
|
|
409
|
+
webpackBuildWorker: true
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
**Step 4: Docker Volume Configuration**
|
|
415
|
+
|
|
416
|
+
Exclude `.next` from volume mounts:
|
|
417
|
+
```yaml
|
|
418
|
+
volumes:
|
|
419
|
+
- .:/app
|
|
420
|
+
- /app/node_modules
|
|
421
|
+
- /app/.next # Don't sync build directory
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
**Step 5: WSL2 on Windows**
|
|
425
|
+
|
|
426
|
+
Enable polling for file watching:
|
|
427
|
+
```bash
|
|
428
|
+
# Set environment variable
|
|
429
|
+
CHOKIDAR_USEPOLLING=true npm run dev
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
### Problem 3: Slow Development Server Startup
|
|
433
|
+
|
|
434
|
+
**Symptoms:**
|
|
435
|
+
- Server takes 30+ seconds to start
|
|
436
|
+
- "Compiling..." messages take forever
|
|
437
|
+
- High CPU usage during startup
|
|
438
|
+
|
|
439
|
+
**Root Cause:**
|
|
440
|
+
Large dependency tree, unoptimized imports, or lack of Turbopack usage.
|
|
441
|
+
|
|
442
|
+
**Solution:**
|
|
443
|
+
|
|
444
|
+
**Step 1: Enable Turbopack**
|
|
445
|
+
```bash
|
|
446
|
+
npm run dev -- --turbo
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
**Step 2: Optimize Imports**
|
|
450
|
+
|
|
451
|
+
Bad:
|
|
452
|
+
```javascript
|
|
453
|
+
import { Button } from '@mui/material';
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
Good:
|
|
457
|
+
```javascript
|
|
458
|
+
import Button from '@mui/material/Button';
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
**Step 3: Configure Barrel File Optimization**
|
|
462
|
+
```javascript
|
|
463
|
+
// next.config.js
|
|
464
|
+
module.exports = {
|
|
465
|
+
experimental: {
|
|
466
|
+
optimizePackageImports: ['@mui/material', 'lodash', 'react-icons']
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
**Step 4: Exclude Unnecessary Files**
|
|
472
|
+
```javascript
|
|
473
|
+
// next.config.js
|
|
474
|
+
module.exports = {
|
|
475
|
+
pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],
|
|
476
|
+
// This prevents Next.js from treating all files as routes
|
|
477
|
+
};
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
**Step 5: Increase Node Memory**
|
|
481
|
+
```json
|
|
482
|
+
{
|
|
483
|
+
"scripts": {
|
|
484
|
+
"dev": "NODE_OPTIONS='--max-old-space-size=4096' next dev --turbo"
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
### Problem 4: PM2 Breaks Development Workflow
|
|
490
|
+
|
|
491
|
+
**Symptoms:**
|
|
492
|
+
- HMR stops working with PM2
|
|
493
|
+
- File changes not detected
|
|
494
|
+
- Need to restart PM2 after every change
|
|
495
|
+
|
|
496
|
+
**Root Cause:**
|
|
497
|
+
PM2 watch mode interferes with Next.js file watching, causing conflicts and breaking HMR.
|
|
498
|
+
|
|
499
|
+
**Solution:**
|
|
500
|
+
|
|
501
|
+
**Never use PM2 for Next.js development. Always use direct process.**
|
|
502
|
+
|
|
503
|
+
**Development (Correct):**
|
|
504
|
+
```bash
|
|
505
|
+
npm run dev
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
**Production (Correct):**
|
|
509
|
+
```bash
|
|
510
|
+
npm run build
|
|
511
|
+
pm2 start npm --name "nextjs" -- start
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
**Development with PM2 (Wrong - Don't Do This):**
|
|
515
|
+
```bash
|
|
516
|
+
# WRONG - Breaks HMR
|
|
517
|
+
pm2 start npm --name "nextjs-dev" --watch -- run dev
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
If you need process management in development:
|
|
521
|
+
- Use `nodemon` for auto-restart on crashes
|
|
522
|
+
- Use terminal multiplexers (`tmux`, `screen`)
|
|
523
|
+
- Use IDE run configurations
|
|
524
|
+
- NOT PM2
|
|
525
|
+
|
|
526
|
+
### Problem 5: Environment Variables Not Loading
|
|
527
|
+
|
|
528
|
+
**Symptoms:**
|
|
529
|
+
- `process.env.MY_VAR` is undefined
|
|
530
|
+
- Variables work in production but not development
|
|
531
|
+
- Public variables not accessible in browser
|
|
532
|
+
|
|
533
|
+
**Root Cause:**
|
|
534
|
+
Incorrect file naming, missing `NEXT_PUBLIC_` prefix, or variables added after server start.
|
|
535
|
+
|
|
536
|
+
**Solution:**
|
|
537
|
+
|
|
538
|
+
**Step 1: Correct File Naming**
|
|
539
|
+
```
|
|
540
|
+
.env.local ← Use this for development
|
|
541
|
+
.env.development ← Optional, environment-specific
|
|
542
|
+
.env ← Shared defaults, committed to git
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
**Step 2: Restart Server**
|
|
546
|
+
|
|
547
|
+
Environment variables are loaded on server start:
|
|
548
|
+
```bash
|
|
549
|
+
# Restart required after .env changes
|
|
550
|
+
npm run dev
|
|
551
|
+
```
|
|
552
|
+
|
|
553
|
+
**Step 3: Public vs Private Variables**
|
|
554
|
+
|
|
555
|
+
Server-side only:
|
|
556
|
+
```bash
|
|
557
|
+
API_SECRET=secret123
|
|
558
|
+
```
|
|
559
|
+
|
|
560
|
+
Browser accessible:
|
|
561
|
+
```bash
|
|
562
|
+
NEXT_PUBLIC_API_URL=https://api.example.com
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
**Step 4: Runtime Access**
|
|
566
|
+
```javascript
|
|
567
|
+
// Server Component or API Route
|
|
568
|
+
const secret = process.env.API_SECRET;
|
|
569
|
+
|
|
570
|
+
// Client Component
|
|
571
|
+
const apiUrl = process.env.NEXT_PUBLIC_API_URL;
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
**Step 5: Validate Loading**
|
|
575
|
+
```javascript
|
|
576
|
+
// pages/api/debug.js
|
|
577
|
+
export default function handler(req, res) {
|
|
578
|
+
res.json({
|
|
579
|
+
nodeEnv: process.env.NODE_ENV,
|
|
580
|
+
port: process.env.PORT,
|
|
581
|
+
// Don't expose secrets in real debug endpoints
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
## Anti-Patterns
|
|
587
|
+
|
|
588
|
+
### What NOT to Do
|
|
589
|
+
|
|
590
|
+
**1. Never Use PM2 for Development Server**
|
|
591
|
+
```bash
|
|
592
|
+
# WRONG - Breaks HMR and file watching
|
|
593
|
+
pm2 start npm --name "dev" --watch -- run dev
|
|
594
|
+
```
|
|
595
|
+
|
|
596
|
+
Why: PM2's watch mode conflicts with Next.js file watching, breaking HMR completely.
|
|
597
|
+
|
|
598
|
+
**2. Don't Ignore Build Artifacts in Docker**
|
|
599
|
+
```yaml
|
|
600
|
+
# WRONG - Causes build issues
|
|
601
|
+
volumes:
|
|
602
|
+
- .:/app
|
|
603
|
+
```
|
|
604
|
+
|
|
605
|
+
Correct:
|
|
606
|
+
```yaml
|
|
607
|
+
volumes:
|
|
608
|
+
- .:/app
|
|
609
|
+
- /app/node_modules
|
|
610
|
+
- /app/.next
|
|
611
|
+
```
|
|
612
|
+
|
|
613
|
+
**3. Don't Use Same Port for Multiple Services**
|
|
614
|
+
```bash
|
|
615
|
+
# WRONG - Causes port conflicts
|
|
616
|
+
PORT=3000 npm run dev # Next.js
|
|
617
|
+
PORT=3000 python api.py # API server
|
|
618
|
+
```
|
|
619
|
+
|
|
620
|
+
Use distinct ports: 3000 (Next.js), 4000 (API), 5432 (Database), etc.
|
|
621
|
+
|
|
622
|
+
**4. Don't Commit .env.local**
|
|
623
|
+
```
|
|
624
|
+
# WRONG in .gitignore
|
|
625
|
+
# .env.local ← Should NOT be commented out
|
|
626
|
+
```
|
|
627
|
+
|
|
628
|
+
Correct:
|
|
629
|
+
```
|
|
630
|
+
# .gitignore
|
|
631
|
+
.env.local
|
|
632
|
+
.env*.local
|
|
633
|
+
```
|
|
634
|
+
|
|
635
|
+
**5. Don't Skip Production Build**
|
|
636
|
+
```bash
|
|
637
|
+
# WRONG for production
|
|
638
|
+
pm2 start npm --name "prod" -- run dev
|
|
639
|
+
```
|
|
640
|
+
|
|
641
|
+
Correct:
|
|
642
|
+
```bash
|
|
643
|
+
npm run build
|
|
644
|
+
pm2 start npm --name "prod" -- start
|
|
645
|
+
```
|
|
646
|
+
|
|
647
|
+
**6. Don't Use require() for Next.js Config**
|
|
648
|
+
```javascript
|
|
649
|
+
// WRONG - May cause issues
|
|
650
|
+
const withPlugins = require('next-compose-plugins');
|
|
651
|
+
```
|
|
652
|
+
|
|
653
|
+
Prefer ES modules (Next.js 13+):
|
|
654
|
+
```javascript
|
|
655
|
+
// next.config.mjs
|
|
656
|
+
export default {
|
|
657
|
+
// config
|
|
658
|
+
};
|
|
659
|
+
```
|
|
660
|
+
|
|
661
|
+
**7. Don't Ignore Memory Limits**
|
|
662
|
+
```bash
|
|
663
|
+
# WRONG for large projects
|
|
664
|
+
npm run dev
|
|
665
|
+
```
|
|
666
|
+
|
|
667
|
+
Correct:
|
|
668
|
+
```bash
|
|
669
|
+
NODE_OPTIONS='--max-old-space-size=4096' npm run dev
|
|
670
|
+
```
|
|
671
|
+
|
|
672
|
+
## Quick Reference
|
|
673
|
+
|
|
674
|
+
### Commands
|
|
675
|
+
|
|
676
|
+
```bash
|
|
677
|
+
# Development
|
|
678
|
+
npm run dev # Start dev server
|
|
679
|
+
npm run dev -- -p 3001 # Custom port
|
|
680
|
+
npm run dev -- --turbo # With Turbopack
|
|
681
|
+
NEXT_TURBOPACK_TRACING=1 npm run dev # Debug Turbopack
|
|
682
|
+
|
|
683
|
+
# Production
|
|
684
|
+
npm run build # Create production build
|
|
685
|
+
npm start # Start production server
|
|
686
|
+
npm start -- -p 3001 # Production on custom port
|
|
687
|
+
|
|
688
|
+
# Maintenance
|
|
689
|
+
rm -rf .next # Clear build cache
|
|
690
|
+
rm -rf node_modules/.cache # Clear dependency cache
|
|
691
|
+
lsof -ti:3000 | xargs kill -9 # Kill process on port 3000
|
|
692
|
+
|
|
693
|
+
# PM2 (Production Only)
|
|
694
|
+
pm2 start npm --name "nextjs" -- start # Start with PM2
|
|
695
|
+
pm2 restart nextjs # Restart
|
|
696
|
+
pm2 stop nextjs # Stop
|
|
697
|
+
pm2 logs nextjs # View logs
|
|
698
|
+
pm2 delete nextjs # Remove from PM2
|
|
699
|
+
```
|
|
700
|
+
|
|
701
|
+
### Configuration Templates
|
|
702
|
+
|
|
703
|
+
**Minimal next.config.js:**
|
|
704
|
+
```javascript
|
|
705
|
+
/** @type {import('next').NextConfig} */
|
|
706
|
+
const nextConfig = {
|
|
707
|
+
reactStrictMode: true,
|
|
708
|
+
swcMinify: true,
|
|
709
|
+
};
|
|
710
|
+
|
|
711
|
+
module.exports = nextConfig;
|
|
712
|
+
```
|
|
713
|
+
|
|
714
|
+
**Development-Optimized next.config.js:**
|
|
715
|
+
```javascript
|
|
716
|
+
/** @type {import('next').NextConfig} */
|
|
717
|
+
const nextConfig = {
|
|
718
|
+
reactStrictMode: true,
|
|
719
|
+
swcMinify: true,
|
|
720
|
+
|
|
721
|
+
experimental: {
|
|
722
|
+
optimizePackageImports: ['lodash', '@mui/material', 'react-icons'],
|
|
723
|
+
webpackBuildWorker: true,
|
|
724
|
+
},
|
|
725
|
+
|
|
726
|
+
// For reverse proxy setups
|
|
727
|
+
assetPrefix: process.env.ASSET_PREFIX || '',
|
|
728
|
+
|
|
729
|
+
// Logging
|
|
730
|
+
logging: {
|
|
731
|
+
fetches: {
|
|
732
|
+
fullUrl: true,
|
|
733
|
+
},
|
|
734
|
+
},
|
|
735
|
+
};
|
|
736
|
+
|
|
737
|
+
module.exports = nextConfig;
|
|
738
|
+
```
|
|
739
|
+
|
|
740
|
+
**Production PM2 Config:**
|
|
741
|
+
```javascript
|
|
742
|
+
module.exports = {
|
|
743
|
+
apps: [{
|
|
744
|
+
name: 'nextjs-prod',
|
|
745
|
+
script: 'node_modules/next/dist/bin/next',
|
|
746
|
+
args: 'start',
|
|
747
|
+
instances: 'max',
|
|
748
|
+
exec_mode: 'cluster',
|
|
749
|
+
env: {
|
|
750
|
+
NODE_ENV: 'production',
|
|
751
|
+
PORT: 3000
|
|
752
|
+
},
|
|
753
|
+
max_memory_restart: '1G',
|
|
754
|
+
autorestart: true,
|
|
755
|
+
watch: false,
|
|
756
|
+
error_file: './logs/err.log',
|
|
757
|
+
out_file: './logs/out.log',
|
|
758
|
+
log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
|
|
759
|
+
}]
|
|
760
|
+
};
|
|
761
|
+
```
|
|
762
|
+
|
|
763
|
+
**Docker Compose (Full Stack):**
|
|
764
|
+
```yaml
|
|
765
|
+
version: '3.8'
|
|
766
|
+
|
|
767
|
+
services:
|
|
768
|
+
nextjs:
|
|
769
|
+
build: .
|
|
770
|
+
ports:
|
|
771
|
+
- "3000:3000"
|
|
772
|
+
environment:
|
|
773
|
+
- NODE_ENV=development
|
|
774
|
+
- DATABASE_URL=postgresql://postgres:password@db:5432/mydb
|
|
775
|
+
volumes:
|
|
776
|
+
- .:/app
|
|
777
|
+
- /app/node_modules
|
|
778
|
+
- /app/.next
|
|
779
|
+
depends_on:
|
|
780
|
+
- db
|
|
781
|
+
|
|
782
|
+
db:
|
|
783
|
+
image: postgres:15-alpine
|
|
784
|
+
environment:
|
|
785
|
+
- POSTGRES_PASSWORD=password
|
|
786
|
+
- POSTGRES_DB=mydb
|
|
787
|
+
ports:
|
|
788
|
+
- "5432:5432"
|
|
789
|
+
volumes:
|
|
790
|
+
- postgres_data:/var/lib/postgresql/data
|
|
791
|
+
|
|
792
|
+
volumes:
|
|
793
|
+
postgres_data:
|
|
794
|
+
```
|
|
795
|
+
|
|
796
|
+
## Related Skills
|
|
797
|
+
|
|
798
|
+
- **docker-containerization** - For containerized Next.js deployments
|
|
799
|
+
- **systematic-debugging** - For complex debugging scenarios
|
|
800
|
+
- **vite-local-dev** - Similar patterns for Vite-based React applications
|
|
801
|
+
- **express-local-dev** - For Next.js custom server implementations
|
|
802
|
+
|
|
803
|
+
---
|
|
804
|
+
|
|
805
|
+
**Next.js Version Compatibility:** This skill covers Next.js 13+ with App Router and Turbopack. For Pages Router or older versions, some patterns may differ. Always consult official Next.js documentation for version-specific features.
|
|
806
|
+
|
|
807
|
+
**Last Updated:** 2024 - Reflects stable Turbopack integration and current Next.js best practices.
|