Sep 29, 2015

React on Rails with webpack - React is not defined

I'm using the react on rails gem in a project I've been working on. It's great for using of React components in a Rails project and supports both server side and client side rendering.

If you've got webpack set up to work as part of your rails stack (example) the gem is a breeze to set up.

I did run into 1 problem recently though. I was getting an error when I deployed to heroku that I wasn't getting in dev mode.

Exception in rendering!
ReferenceError: React is not defined

To fix this, make sure you are exposing react in your webpack config file.

module.exports = {
  ...
  module: {
    loaders: [
      // Make sure you have this line in your config!
      {test: require.resolve('react'), loader: 'expose?React'},
      ...
    ]
  }
};