create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

Rails API with Create-React-App on Heroku

Open vldsoar opened this issue 9 years ago • 6 comments

Original post: http://stackoverflow.com/questions/39774143/rails-api-with-create-react-app-on-heroku-npm-command-not-found

I created Rails API backend app and inside the app in a folder client I created create-react-app for my frontend. It works well on my local machine but does not work on heroku with an error:

npm: command not found

This is the structure of the app:

rails-app

  • app
    • controller
    • model
    • ...
  • client
    • package.json
    • index.jsx
    • ...
  • config
  • db
  • ...
  • Procfile

Inside Procfile I have:

web: cd client && npm start api: bundle exec rails s -p 3001

vldsoar avatar Jan 06 '17 20:01 vldsoar

I don't know much about Heroku but presumably your server doesn't have Node.js?

gaearon avatar Jan 06 '17 21:01 gaearon

Yes! A example: https://www.fullstackreact.com/articles/how-to-get-create-react-app-to-work-with-your-rails-api/

The structure is pretty much the same.

vldsoar avatar Jan 06 '17 22:01 vldsoar

@vldsoar The Rails core team are looking to merge Webpacker when it's ready, most likely in Rails 5.1

I'd recommend deploying your frontend and API separately. The API version of Rails does a great job in removing any "view layer" dependencies.

notrab avatar Jan 09 '17 10:01 notrab

Update: @vldsoar If this PR gets merged then this could be closed @gaearon ?

That tutorial walks you through step by step.

notrab avatar Jan 10 '17 09:01 notrab

I have created a project using create react app and the backend rails api is on different server and i am facing cross origin unable hit any post api from

shabanraza avatar Jan 11 '17 15:01 shabanraza

@shabanraza While this isn't a Create React App issue, you might want to checkout the rack cors gem.

Maybe this can be closed @gaearon ?

notrab avatar Jan 11 '17 16:01 notrab