title: Using Node.js modules in HTML
body: <p>I have the following Node.js project (which is a Minimal Working Example of my problem):</p>
  
  <p><strong>module1.js</strong>:</p>
  
  <pre><code>module.exports = function() {
      return "this is module1!";
  };
  </code></pre>
  
  <p><strong>module2.js</strong>:</p>
  
  <pre><code>var module1 = require('./module1');
  module.exports = function() {
      return module1()+" and this is module2!";
  };
  </code></pre>
  
  <p><strong>server.js</strong>:</p>
  
  <pre><code>var module2 = require('./module2');
  console.log(module2());  // prints: "this is module1! and this is module2!"
  </code></pre>
  
  <p>Now I want to create a <strong>client.html</strong> file that will also use module2.js. Here is what I tried (and failed):</p>
  
  <p><strong>naive version</strong>:</p>
  
  <pre><code>&lt;script src='module2.js'&gt;&lt;/script&gt;
  &lt;script&gt;alert(module2());&lt;/script&gt; // should alert: "this is module1! and this is module2!"
  </code></pre>
  
  <p>This obviously doesn't work - it produces two errors:</p>
  
  <ul>
  <li>ReferenceError: require is not defined.</li>
  <li>ReferenceError: module2 is not defined.</li>
  </ul>
  
  <p><strong>Using <a href="https://github.com/substack/node-browserify" rel="noreferrer">Node-Browserify</a></strong>: After running:</p>
  
  <pre><code>browserify module2.js &gt; module2.browserified.js
  </code></pre>
  
  <p>I changed client.html to:</p>
  
  <pre><code>&lt;script src='require.js'&gt;&lt;/script&gt;
  &lt;script&gt;
      var module2 = require('module2');
      alert(module2());
  &lt;/script&gt;
  </code></pre>
  
  <p>This doesn't work - it produces one error:</p>
  
  <ul>
  <li>ReferenceError: module2 is not defined.</li>
  </ul>
  
  <p><strong>Using <a href="http://pixelsvsbytes.com/blog/2013/02/js-require-for-browsers-better-faster-stronger/" rel="noreferrer">Smoothie.js</a></strong> by @Torben :</p>
  
  <pre><code>&lt;script src='require.js'&gt;&lt;/script&gt;
  &lt;script&gt;
      var module2 = require('module2');
      alert(module2());
  &lt;/script&gt;
  </code></pre>
  
  <p>This doesn't work - it produces three errors:</p>
  
  <ul>
  <li>syntax error on module2.js line 1.</li>
  <li>SmoothieError: unable to load module2 (0 )</li>
  <li>TypeError: module2 is not a function</li>
  </ul>
  
  <p>I looked at <a href="http://requirejs.org/docs/node.html" rel="noreferrer">require.js</a> but it looks too complicated to combine with Node.js - I didn't find a simple example that just takes an existing Node.js module and loads it into a web page (like in the example).</p>
  
  <p>I looked at <a href="https://github.com/headjs/headjs" rel="noreferrer">head.js</a> and <a href="http://labjs.com/" rel="noreferrer">lab.js</a> but found no mention of Node.js's require.</p>
  
  <p>So, what should I do in order to use my existing Node.js module, module2.js, from an HTML page?</p>