RequireJS and require
are completely different things - more on this later.
If you want to use React without a tool like Browserify or Webpack, you do not need a module loader. Hosted versions of React and ReactDOM will display global variables that you can use out of the box.
<script src="https://fb.me/react-0.14.7.js"></script> <script src="https://fb.me/react-dom-0.14.7.js"></script> <script> console.log(React, ReactDOM); </script>
Just download these files if you want to work with them locally.
Systemjs
From all this, it seems that SystemJS and JSPM may be exactly what you are looking for.
First use jspm
to install your packages.
jspm install systemjs react react-dom
Then connect and configure SystemJS.
<script src='jspm_packages/system.js'></script> <script> System.import('app.js'); </script>
Now inside app.js
you can write CommonJS style code.
var React = require('react'); var ReactDOM = require('react-dom');
SystemJS will handle loading the rest of your scripts. If you want to create an assembly, it will be as simple as running the jspm bundle app
.
Commonjs
The require
call, which you see in React tutorials and other examples, refers to the format of the CommonJS module.
You use require('react')
to get a reference to the value exported by the React module (set to node_modules
with npm number). This means that you need a pre-browser step, such as Browserify or Webpack, that can stitch all the modules you need and output one large script file.
RequireJS
Vaguely, CommonJS and RequireJS use a function with the same name to indicate dependencies. You are trying to use the RequireJS require
function, as if you were working with CommonJS modules.
If you want to import React with RequireJS instead, you need something like this:
<script src="js/require.js"></script> <script> require.config({ 'baseUrl' : 'Scripts/', }); require(["react-0.14.7", "react-dom-0.14.7"], function(React, ReactDOM) { console.log(React, ReactDOM); }); </script>
When your code executes, RequireJS will shut down and add script tags for the modules you specify. Then, once these scripts are loaded, they will pass the values ββthat they export to the callback function that you can use.