Search code examples
javascriptreactjsbrowserify

Browserify produces an unusable bundle.js


I'm trying to get a very simple React app to work. I want to require() React, so I'm using browserify to produce a bundle.js:

browserify -t babelify libs.js -o bundle.js
browserify -t reactify app.jsx -o app.js

When the page loads, it does not show the React content. On the console, it says: ReferenceError: ReactDOM is not defined

However, when I replace the <script src="bundle.js"> with

<script src="js/react-0.14.0.js"></script>
<script src="js/react-dom-0.14.0.js"></script>

everything works. I'm sure I'm missing something obvious. I'd appreciate hints as to what I'm doing wrong.

helloworld.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <script src="bundle.js"></script>
  </head>
  <body>
    <div id="example"></div>
    <script src="app.js"></script>
  </body>
</html>

app.jsx:

ReactDOM.render(
  <h1>Hello, world!</h1>,
  document.getElementById('example')
);

libs.js:

var React = require('react');
var ReactDOM = require('react-dom');

Solution

  • app.jsx should have the requires.

    var React = require('react');
    var ReactDOM = require('react-dom');
    
    ReactDOM.render(
      <h1>Hello, world!</h1>,
      document.getElementById('example')
    );