requirejs - Requiring modules gives module not loaded error -


i trying write simple hello world app in react.js using component based approach. using requie.js. have 4 files in same folder namely index.html, index.js,world.js , require.js. having script tag in index.html load index.js. loading world.js via require.js using module.exports, result in error. here code
index.html

<head> <script src="https://fb.me/react-0.13.3.js"></script> <!-- in-browser jsx transformer, remove when pre-compiling jsx. --> <script src="https://fb.me/jsxtransformer-0.13.3.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <script data-main="index.js" src="require.js"></script>  </script>   </head>  <body>    <script type="text/jsx" src="index.js"></script> </body> 


index.js

var world = require('./world');  var hello = react.createclass({   render:function(){     return (<div>         <div>hello,</div>         <world/>       </div>)    }  })  var element = react.createelement(hello);  react.render(element,document.body); 

world.js

module.exports = react.createclass({   render: function(){     return (<div>world!</div)   }  }) 

i intending show hello, world. i'm getting following errors

uncaught syntaxerror: unexpected token < fbcdn-dragon-a.akamaihd.net/hphotos-ak-xtf1/t39.3284-6/11057100_835863049837306_1087123501_n.js:314 using in-browser jsx transformer. sure precompile jsx production - http://facebook.github.io/react/docs/tooling-integration.html#jsx require.js:8 uncaught error: module name "world" has not been loaded yet context: _. use require([]) http://requirejs.org/docs/errors.html#notloaded :8000/index.js:5 uncaught syntaxerror: unexpected token < 

there @ least 3 issues here. first, not using correct require syntax asynchronous loading. index.js should be:

define(['world'], function(world) {      var hello = react.createclass({         render:function(){             return (<div>                     <div>hello,</div>                     <world/>                     </div>)          }      })      var element = react.createelement(hello);      react.render(element,document.body); }); 

second, since index.js , world.js jsx files, requirejs needs plugin tell that. like:

https://github.com/philix/jsx-requirejs-plugin

finally, since loading index.js via requirejs, don't need:

<script type="text/jsx" src="index.js"></script> 

Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -