Skip to content

Commit 3cef885

Browse files
committed
Fix critical installation order in AI agent instructions
🚨 BREAKING: Fixed installation sequence that was causing errors **Root Cause Found:** - React on Rails generator requires package.json to exist - Rails 8 with --skip-javascript doesn't create package.json - Previous instructions tried React on Rails first → package.json error **Fix Applied:** 1. ✅ Always install Shakapacker FIRST (creates package.json) 2. ✅ Then install React on Rails (can add JS dependencies) **Updated All 3 Scenarios:** - New Rails App: Shakapacker → React on Rails - Existing Rails App: Shakapacker → React on Rails - Vite Migration: Remove Vite → Shakapacker → React on Rails **Validated:** - Created test app following new instructions → SUCCESS - React component renders, HMR works, no errors - Updated to current gem versions (15.0, 8.3) This fix prevents the "package.json not found" error that breaks setup.
1 parent 5e48745 commit 3cef885

File tree

1 file changed

+47
-34
lines changed

1 file changed

+47
-34
lines changed

AI_AGENT_INSTRUCTIONS.md

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,49 @@ gem search shakapacker --remote
1111

1212
# Or use specific versions from these commands in your Gemfile:
1313
# Latest stable versions as of Jan 2025:
14-
# react_on_rails ~> 14.2
15-
# shakapacker ~> 8.1
14+
# react_on_rails ~> 15.0
15+
# shakapacker ~> 8.3
1616
```
1717

1818
**⚠️ Version Flexibility:** These instructions use `~> X.Y` which allows patch updates. Always check for latest versions before starting a new project.
1919

20+
## 🚨 **CRITICAL: Installation Order Matters**
21+
22+
**ALWAYS install Shakapacker FIRST, then React on Rails. Here's why:**
23+
24+
1. **React on Rails generator requires `package.json`** to add JavaScript dependencies
25+
2. **Rails with `--skip-javascript` doesn't create `package.json`**
26+
3. **Shakapacker creates `package.json`** and JavaScript tooling foundation
27+
4. **Wrong order = "package.json not found" error**
28+
29+
**✅ Correct Order:**
30+
```
31+
Shakapacker → package.json created → React on Rails → success
32+
```
33+
34+
**❌ Wrong Order:**
35+
```
36+
React on Rails → no package.json → ERROR: package.json not found
37+
```
38+
2039
---
2140

2241
## 🆕 Scenario 1: New Rails App with React on Rails
2342

2443
```bash
25-
# Create new Rails app with essential gems
44+
# Create new Rails app
2645
rails new myapp --skip-javascript --database=postgresql
2746
cd myapp
2847

29-
# Add React on Rails to Gemfile (latest versions)
30-
echo 'gem "react_on_rails", "~> 14.2"' >> Gemfile
31-
echo 'gem "shakapacker", "~> 8.1"' >> Gemfile
48+
# STEP 1: Add Shakapacker first (creates package.json)
49+
echo 'gem "shakapacker", "~> 8.3"' >> Gemfile
3250
bundle install
51+
bundle exec rails shakapacker:install
3352

34-
# Install React on Rails with Node dependencies
53+
# STEP 2: Add React on Rails (requires package.json to exist)
54+
echo 'gem "react_on_rails", "~> 15.0"' >> Gemfile
55+
bundle install
3556
rails generate react_on_rails:install
36-
yarn install
3757

3858
# Start development servers
3959
bin/dev
@@ -54,23 +74,20 @@ bin/dev
5474
# Navigate to existing Rails app root
5575
cd /path/to/existing/app
5676

57-
# Add gems to Gemfile (before final 'end')
58-
cat >> Gemfile << 'EOF'
77+
# STEP 1: Add Shakapacker first (creates package.json if missing)
78+
echo 'gem "shakapacker", "~> 8.3"' >> Gemfile
79+
bundle install
5980

60-
# React on Rails
61-
gem "react_on_rails", "~> 14.2"
62-
gem "shakapacker", "~> 8.1"
63-
EOF
81+
# Check if package.json exists, create if missing
82+
if [ ! -f "package.json" ]; then
83+
bundle exec rails shakapacker:install
84+
fi
6485

65-
# Install gems
86+
# STEP 2: Add React on Rails (requires package.json to exist)
87+
echo 'gem "react_on_rails", "~> 15.0"' >> Gemfile
6688
bundle install
67-
68-
# Install React on Rails (will not overwrite existing files)
6989
rails generate react_on_rails:install --ignore-existing-files
7090

71-
# Install Node dependencies
72-
yarn install
73-
7491
# Add React component to existing view
7592
# Replace <view-name> with your actual view file
7693
cat >> app/views/<view-name>/<action>.html.erb << 'EOF'
@@ -84,7 +101,7 @@ bin/dev
84101

85102
**⚠️ Pre-flight Checks:**
86103
- Rails app has `bin/dev` or similar dev script
87-
- `package.json` exists (if not, run `yarn init -y` first)
104+
- Shakapacker will create `package.json` if it doesn't exist
88105
- No existing React setup conflicts
89106

90107
**✅ Success Check:** React component renders in your chosen view
@@ -101,25 +118,21 @@ cd /path/to/vite/ruby/app
101118
sed -i.bak '/gem.*vite_rails/d' Gemfile
102119
sed -i.bak '/gem.*vite_ruby/d' Gemfile
103120

104-
# Add React on Rails gems to Gemfile
105-
cat >> Gemfile << 'EOF'
106-
107-
# React on Rails (replacing Vite)
108-
gem "react_on_rails", "~> 14.2"
109-
gem "shakapacker", "~> 8.1"
110-
EOF
111-
112-
# Install new gems
113-
bundle install
114-
115121
# Backup existing Vite config
116122
mv vite.config.* vite.config.backup 2>/dev/null || true
117123

118124
# Remove Vite-specific files
119125
rm -rf config/vite.json
120126
rm -rf bin/vite*
121127

122-
# Install React on Rails
128+
# STEP 1: Add Shakapacker first (creates package.json)
129+
echo 'gem "shakapacker", "~> 8.3"' >> Gemfile
130+
bundle install
131+
bundle exec rails shakapacker:install --force
132+
133+
# STEP 2: Add React on Rails (requires package.json to exist)
134+
echo 'gem "react_on_rails", "~> 15.0"' >> Gemfile
135+
bundle install
123136
rails generate react_on_rails:install --force
124137

125138
# Migrate existing React components
@@ -210,7 +223,7 @@ app/
210223

211224
### Version Requirements
212225
- Rails 7+ (Rails 8 supported), Ruby 3.0+ (Ruby 3.2+ for Rails 8), Node 20+ LTS, Yarn
213-
- react_on_rails ~> 14.2+, shakapacker ~> 8.1+
226+
- react_on_rails ~> 15.0+, shakapacker ~> 8.3+
214227
- **Note**: Use `bundle info react_on_rails` to check latest available version
215228

216229
---

0 commit comments

Comments
 (0)