Node output running the code shown below is:
all Loaded
dbModule undefined
mongoose loaded
I have trouble getting a reference to dbMod2 in appMod2. What should I try?
//server.js
var requirejs = require( 'requirejs');
process.on( 'uncaughtException', function(error) {
console.log( 'Exception ' + error.stack);
process.exit( 1);
});
requirejs.config({
baseUrl: '.',
nodeRequire: require
});
requirejs(['./appMod2'], function() {
console.log( "all Loaded");
})
// appMod2.js
require(['./dbmod2'], function( dbmodule) {
console.log( "dbModule " + dbmodule);
});
// dbmod2.js
require(['mongoose'], function( mongoose) {
Schema = mongoose.Schema;
console.log( "mongoose loaded");
});
for replacing 'require' calls with 'define' and adding return statement to the modules:
// dbmod2.js
define(['mongoose'], function( mongoose) {
Schema = mongoose.Schema;
console.log( "mongoose loaded");
return 'dbmod2';
});
// appMod2.js
define(['./dbmod2'], function( dbmodule) {
console.log( "dbModule " + dbmodule);
return 'appMod2';
});
I get this output:
mongoose loaded
dbModule dbmod2
all Loaded
in requirejs, the object passed to a the function following the dependency array is the object returned by the dependency