title: Why do I see "define not defined" when running a Mocha test with RequireJS?
body: <p>I am trying to understand how to develop stand-alone Javascript code.  I want to write Javscript code with tests and modules, running from the command line.  So I have installed <code>node.js</code> and <code>npm</code> along with the libraries <code>requirejs</code>, <code>underscore</code>, and <code>mocha</code>.</p>
  
  <p>My directory structure looks like this:</p>
  
  <pre><code>&gt; tree .
  .
  ├── node_modules
  ├── src
  │   └── utils.js
  └── test
      └── utils.js
  </code></pre>
  
  <p>where <code>src/utils.js</code> is a little module that I am writing, with the following code:</p>
  
  <pre><code>&gt; cat src/utils.js 
  define(['underscore'], function () {
  
      "use strict";
  
      if ('function' !== typeof Object.beget) {
          Object.beget = function (o) {
              var f = function () {
              };
              f.prototype = o;
              return new f();
          };
      }
  
  });
  </code></pre>
  
  <p>and <code>test/utils.js</code> is the test:</p>
  
  <pre><code>&gt; cat test/utils.js 
  var requirejs = require('requirejs');
  requirejs.config({nodeRequire: require});
  
  requirejs(['../src/utils'], function(utils) {
  
      suite('utils', function() {
          test('should always work', function() {
              assert.equal(1, 1);
          })
      })
  
  });
  </code></pre>
  
  <p>which I then try to run from the top level directory (so <code>mocha</code> sees the <code>test</code> directory):</p>
  
  <pre><code>&gt; mocha
  
  node.js:201
          throw e; // process.nextTick error, or 'error' event on first tick
                ^
  Error: Calling node's require("../src/utils") failed with error: ReferenceError: define is not defined
      at /.../node_modules/requirejs/bin/r.js:2276:27
      at Function.execCb (/.../node_modules/requirejs/bin/r.js:1872:25)
      at execManager (/.../node_modules/requirejs/bin/r.js:541:31)
      ...
  </code></pre>
  
  <p>So my questions are:</p>
  
  <ul>
  <li>Is this the correct way to structure code?</li>
  <li>Why is my test not running?</li>
  <li>What is the best way to learn this kind of thing?  I am having a hard time finding good examples with Google.</li>
  </ul>
  
  <p>Thanks...</p>
  
  <p>[sorry - momentarily posted results from wrong code; fixed now]</p>
  
  <p>PS I am using requirejs because I also want to run this code (or some of it) from a browser, later.</p>
  
  <p><strong>Update / Solution</strong></p>
  
  <p>Something that is not in the answers below is that I needed to use <code>mocha -u tdd</code> for the test style above.  Here is the final test (which also requires assert) and its use:</p>
  
  <pre><code>&gt; cat test/utils.js 
  
  var requirejs = require('requirejs');
  requirejs.config({nodeRequire: require});
  
  requirejs(['../src/utils', 'assert'], function(utils, assert) {
  
      suite('utils', function() {
          test('should always work', function() {
              assert.equal(1, 1);
          })
      })
  
  });
  &gt; mocha -u tdd
  
    .
  
    ✔ 1 tests complete (1ms)
  </code></pre>