Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions docs/contributor-info/coding-agents-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This guide provides structured instructions for AI coding agents working with Re
### Version Compatibility Matrix

| react_on_rails | Shakapacker | Webpack | Node.js | Ruby |
| -------------- | ----------- | ------- | ------- | ---- |
|----------------|-------------|---------|---------|------|
| v16.x | >= 6.0 | v5 | 20-22 | 3.2+ |
| v14.x | >= 6.0 | v5 | 18-20 | 2.7+ |
| v13.x | >= 6.0 | v5 | 16-18 | 2.7+ |
Expand Down Expand Up @@ -206,29 +206,25 @@ fix_webpack_cache() {
#### 1. Missing Routes File (js-routes gem)

**Detection:**

```regex
/Cannot read properties of undefined.*reading 'module'/
/ProvidedDependencyTemplate\.apply/
```

**Auto-fix:**

```bash
bundle exec rails js:export
```

#### 2. ProvidePlugin Module Missing

**Detection:**

```regex
/Error: Can't resolve.*\$app/
/Module not found.*utils\/routes/
```

**Auto-fix:**

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

**Detection:**

```regex
/webpack.*incompatible/
/peer dep.*react-on-rails/
```

**Auto-fix:**

```bash
# Update to compatible versions
npm install react-on-rails@^16.0.0
Expand Down Expand Up @@ -376,21 +370,18 @@ After successful upgrades, suggest:
### If Build Completely Breaks

1. **Rollback immediately:**

```bash
git checkout HEAD~1 -- Gemfile package.json Gemfile.lock package-lock.json
bundle install
npm install
```

2. **Identify the issue:**

```bash
npm run build 2>&1 | tee build-error.log
```

3. **Apply targeted fixes:**

- Missing routes: `rails js:export`
- Cache issues: `rm -rf node_modules/.cache tmp/cache`
- Dependencies: `bundle update && npm install`
Expand All @@ -400,7 +391,6 @@ After successful upgrades, suggest:
### If Rails Environment Unavailable

Use minimal commands:

```bash
# Skip database operations
DATABASE_URL=sqlite3:tmp/minimal.db rails js:export
Expand Down
1 change: 0 additions & 1 deletion docs/guides/upgrading-react-on-rails.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ rails generate react_on_rails:install
**Symptoms:** Webpack cannot find modules referenced in your configuration

**Solutions:**

1. Clear webpack cache: `rm -rf node_modules/.cache`
2. Verify all ProvidePlugin modules exist
3. Check webpack alias configuration
Expand Down
33 changes: 5 additions & 28 deletions docs/javascript/troubleshooting-build-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,96 +15,80 @@ This guide covers common webpack build errors encountered when using react_on_ra
**Note:** This error only occurs if you're using the optional `js-routes` gem to access Rails routes in JavaScript.

### Error Message

```
Cannot read properties of undefined (reading 'module')
TypeError: Cannot read properties of undefined (reading 'module')
at ProvidedDependencyTemplate.apply
```

### Root Cause

This error occurs when:

1. Your webpack config references Rails routes via ProvidePlugin
2. The `js-routes` gem hasn't generated the JavaScript routes file
3. You're using `js-routes` integration but missing the generated file

### When You Need js-routes

`js-routes` is **optional** and typically used when:

- Rails-heavy apps with React components that need to navigate to Rails routes
- Server-side rendered apps mixing Rails and React routing
- Legacy Rails apps migrating ERB views to React
- Apps using Rails routing patterns for RESTful APIs

### When You DON'T Need js-routes

Most modern React apps use:

- Client-side routing (React Router) instead of Rails routes
- Hardcoded API endpoints or environment variables
- SPA (Single Page App) architecture with API-only Rails backend

### Solution (if using js-routes)

1. **Generate JavaScript routes file:**

```bash
bundle exec rails js:export
```

2. **Verify the routes file was created:**

```bash
ls app/javascript/utils/routes.js
```

3. **Check webpack configuration includes ProvidePlugin:**
```javascript
new webpack.ProvidePlugin({
Routes: '$app/utils/routes',
});
Routes: "$app/utils/routes"
})
```

### Alternative Solution (if NOT using js-routes)

Remove the Routes ProvidePlugin from your webpack configuration:

```javascript
// Remove this line if you don't use js-routes
new webpack.ProvidePlugin({
Routes: '$app/utils/routes', // ← Remove this
});
Routes: "$app/utils/routes" // ← Remove this
})
```

## ProvidePlugin Module Resolution Errors

### Common Error Patterns

- `Cannot read properties of undefined (reading 'module')`
- `Module not found: Error: Can't resolve 'module_name'`
- `ERROR in ./path/to/file.js: Cannot find name 'GlobalVariable'`

### Debugging Steps

1. **Check file existence:**

```bash
find app/javascript -name "routes.*" -type f
find app/javascript -name "*global*" -type f
```

2. **Verify webpack aliases:**

```javascript
// In your webpack config
console.log('Webpack aliases:', config.resolve.alias);
```

3. **Test module resolution:**

```bash
# Run webpack with debug output
bin/shakapacker --debug-shakapacker
Expand All @@ -125,7 +109,6 @@ new webpack.ProvidePlugin({
## Environment Setup Dependencies

### Rails Environment Required

Some operations require a working Rails environment:

- `rails js:export` (generates routes - **only needed if using js-routes gem**)
Expand All @@ -135,20 +118,17 @@ Some operations require a working Rails environment:
### Common Issues

1. **Database Connection Errors:**

```
MONGODB | Error checking localhost:27017: Connection refused
```

**Solution:** These are usually warnings and don't prevent operation. To silence:

```bash
# Run with minimal environment
RAILS_ENV=development bundle exec rails js:export
```

2. **Missing Dependencies:**

```
sidekiq-pro is not installed
```
Expand All @@ -158,7 +138,6 @@ Some operations require a working Rails environment:
### Workarounds

1. **Skip database initialization:**

```bash
DATABASE_URL=sqlite3:tmp/db.sqlite3 rails js:export
```
Expand All @@ -173,15 +152,14 @@ Some operations require a working Rails environment:
### Version Compatibility Matrix

| react_on_rails | Shakapacker | Webpack | Node.js |
| -------------- | ----------- | ------- | ------- |
|----------------|-------------|---------|---------|
| v16.x | >= 6.0 | v5 | 20-22 |
| v14.x | >= 6.0 | v5 | 18-20 |
| v13.x | >= 6.0 | v5 | 16-18 |

### Common Upgrade Issues

1. **Webpacker to Shakapacker migration incomplete:**

```bash
# Remove webpacker references
grep -r "webpacker" config/
Expand All @@ -194,7 +172,6 @@ Some operations require a working Rails environment:
```

### Migration Steps

1. Follow the [Shakapacker upgrade guide](https://github.com/shakacode/shakapacker/blob/main/docs/v6_upgrade.md)
2. Update webpack configurations
3. Regenerate configurations with `rails generate react_on_rails:install`
Expand Down
Loading