small medium large xlarge

Pholser-headshot_pragsmall
24 Mar 2011, 21:44
Paul Holser (2 posts)

I’m working through the examples in the CT book. This is on Cygwin.

With a dir structure of

/lib
    modit.js
    moditTest.js
/spec
    /javascripts
        /specHelper.js
specs.js

and module underscore installed via NPM,

and specHelper.js as:

global.window = global;
global.navigator = {};

require('underscore');
require('modit');
require('moditTest');
require('application');
require('shoppingList');
require('funMenu');

_.extend(exports, global.app);

and with NODE_PATH env var as /usr/local/lib/node:./lib

When I run:

$ node specs.js

I’m getting:

$ node specs.js
(node) process.compile should not be used. Use require('vm').runInThisContext instead.

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
ReferenceError: _ is not defined
    at /cygdrive/c/javascript/scratchpad/nodetests/lib/modit.js:5:5
    at Object.<anonymous> (/cygdrive/c/javascript/scratchpad/nodetests/lib/moditTest.js:1:63)
    at Module._compile (module.js:404:26)
    at Object..js (module.js:410:10)
    at Module.load (module.js:336:31)
    at Function._load (module.js:297:12)
    at require (module.js:348:19)
    at Object.<anonymous> (/cygdrive/c/javascript/scratchpad/nodetests/spec/javascripts/specHelper.js:6:1)
    ...

I was expecting merely to see that module ‘application’ wasn’t found, being that I haven’t created it yet.

Hm. Do I need to explicitly make _ available to modit, via, e.g.:

var u = require(‘underscore’); global._ = u._;

??

Thanks, p

Benrady-avatar_pragsmall
11 May 2011, 04:08
Ben Rady (67 posts)

Yes, that’s exactly what you need to do. I wasn’t using the npm package for underscore in my example, but I’ve updated it to do so.

Benrady-avatar_pragsmall
11 May 2011, 04:12
Ben Rady (67 posts)

However….

If you’re using underscore.js in your production (browser-bound) JavaScript code, and not just the tests, then you don’t want to use the NPM package. You want to use the regular underscore.js file (in the example, we have it in the ‘util’ subdirectory), require it, and let it define _ in the global namespace.

You must be logged in to comment