React - Uncaught ReferenceError: require is not defined
Asked Answered
U

2

14

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

Ultrasonics answered 28/3, 2016 at 3:12 Comment(1)
require only works on front-end client-side code if you're using Browserify or some other solution. Browsers don't have a native require definition.Kingfish
K
15

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.

Kielce answered 28/3, 2016 at 4:9 Comment(2)
The browerify handbook starts with a good explanation of why this is necessary for those who don't understand what this is all about.Decolorize
This answer is four years old. Did native support land?Euphrates
M
-1

Maybe if you try to include components like the first but declare like text/javascript should run.

I hope this solves your problem

Maragretmarala answered 2/2, 2021 at 3:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.