I'm trying to use RequireJS to load server side modules in my node project based on the instructions here: http://requirejs.org/docs/node.html
My boot file looks like this:
var requirejs = require( "requirejs" );
requirejs.config({ nodeRequire: require });
requirejs([ "app" ], function( app ) {
app.listen(80, function(){
console.log( "We be shuffling..." );
});
});
Then, I have an app file like so...
requirejs([
"express"
], function ( express ) {
var app = express();
app.configure( "development", function(){
app.use( express.errorHandler() );
});
return app;
});
...however this just spills out a bunch of errors:
timers.js:103 if (!process.listeners('uncaughtException').length) throw e; ^ TypeError: Cannot call method 'listen' of undefined at /home/ssp/boot.js:13:7 at Object.context.execCb (/home/ssp/node_modules/requirejs/bin/r.js:1729:33) at Object.Module.check (/home/ssp/node_modules/requirejs/bin/r.js:969:51) at Object.Module.enable (/home/ssp/node_modules/requirejs/bin/r.js:1239:22) at Object.Module.init (/home/ssp/node_modules/requirejs/bin/r.js:882:26) at Object.context.makeRequire.mixin.isBrowser [as _onTimeout] (/home/ssp/node_modules/requirejs/bin/r.js:1505:36) at Timer.list.ontimeout (timers.js:101:19) root@nodeapp:/home/ssp# node boot
timers.js:103 if (!process.listeners('uncaughtException').length) throw e; ^ TypeError: Cannot call method 'listen' of undefined at /home/ssp/boot.js:13:7 at Object.context.execCb (/home/ssp/node_modules/requirejs/bin/r.js:1729:33) at Object.Module.check (/home/ssp/node_modules/requirejs/bin/r.js:969:51) at Object.Module.enable (/home/ssp/node_modules/requirejs/bin/r.js:1239:22) at Object.Module.init (/home/ssp/node_modules/requirejs/bin/r.js:882:26) at Object.context.makeRequire.mixin.isBrowser [as _onTimeout] (/home/ssp/node_modules/requirejs/bin/r.js:1505:36) at Timer.list.ontimeout (timers.js:101:19)
Does anyone have any suggestions what I'm missing?
Thanks!
If your intent is to use require with express, why not use the automatic setup script? For instructions on how to set up express using npm, see http://expressjs.com/guide.html#executable Basically, you just use express on the command line. Require is included in the setup.
You need to define() your "module" app instead use requirejs(), because you need to declare your module first.
Add var define = require("amdefine") to your bootfile.
and in your app file :
define(["express" ],
function ( express ) {
var app = express();
app.configure( "development", function(){
app.use( express.errorHandler() );
});
return app;
});