React - Uncaught ReferenceError: require is not defined

16,569

You need to use something like Rollup, Webpack, or Browserify. This statement import FormComponent from './FormComponent.js'; doesn't mean anything on the client. No browser natively supports it so you need something like the tools mentioned above to turn it into something the browser can actually use.

Without them you just have to load the files in your index.html.

Share:
16,569

Related videos on Youtube

André Macedo
Author by

André Macedo

Currently working with React-Redux and Ruby on Rails.

Updated on July 01, 2022

Comments

  • André Macedo
    André Macedo about 2 years

    I'm making a simple flask app, using react for the front-end stuff. Right now I'm experimenting with importing React components from other files, without success. This is the error I'm getting:

    Uncaught ReferenceError: require is not defined

    This is my html file:

    <html>
    <head>
        <meta charset="UTF-8">
        <title>Index page</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
    
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
        <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.0.16/css/bulma.min.css">
        <link rel="stylesheet" type="text/css" href="/static/css/style.css">
    </head>
    <body>
        <div class="columns">
            <div class="column">
                some stuff
            </div>
            <div class="column">
                <div id="index"></div>
            </div>
        </div>
        <script type="text/babel" src="static/js/index.js"></script>
    </body>
    </html>
    

    and my index.js:

    import FormComponent from './FormComponent.js';
    
    var MainClass = React.createClass({
      render:function(){
        return (
          <div>
            <div>this is the main component</div>
            <div><FormComponent /></div>
          </div>
        );
      }
    });
    
    
    ReactDOM.render(<MainClass />, document.getElementById('index'));
    

    and finally the FormCommponent.js, which is in the same folder:

    var FormComponent = React.createClass({
    
      render: function() {
        return (
            <div>this is an imported component</div>
        );
      }
    
    });
    
    module.exports = FormComponent;
    

    Does anyone know what am I doing wrong?

    I'm not using any package managers.

    EDIT
    Solved the problem by using browserify, as mentioned below. Thanks for the help