最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React - Uncaught ReferenceError: require is not defined - Stack Overflow

programmeradmin4浏览0评论

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=".14.7/react.min.js"></script>
    <script type="text/javascript" src=".14.7/react-dom.min.js"></script>
    <script type="text/javascript" src=".8.34/browser.min.js"></script>
    <script type="text/javascript" src=".2.2/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href=".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

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

Share Improve this question edited Feb 27, 2017 at 13:42 André Macedo asked Mar 28, 2016 at 3:12 André MacedoAndré Macedo 1871 gold badge2 silver badges9 bronze badges 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. – Akshat Mahajan Commented Mar 28, 2016 at 3:24
Add a comment  | 

2 Answers 2

Reset to default 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.

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

I hope this solves your problem

发布评论

评论列表(0)

  1. 暂无评论