Search code examples
node.jscompoundjs

compoundjs cannot require file in application controller


We're using CompoundJS to develop an application whose folder structure as follows: compound

compoundApp
|-- app
|   |-- controllers
|   |   |-- application_controller.js
|   |   |-- login_controller.js
|   |-- utils
|   |   |-- email-utils.js  

In the application_controller.js, we require the email-util.js as the following code:

var _ = require('underscore'),
passport = require('passport'),
    EmailUtils = require('./app/utils/email-utils.js')(compound);

We get this error when we start the application:

Error: Cannot find module './app/utils/email-utils.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Promise.eval (eval at <anonymous> (d:\TiniPlanet_SVN\src\TiniGame\trunk\tiNiWeb\node_module\compound\node_modules\kontroller\lib\base.js:157:17), <anonymous>:145:22)

We got the same issue with different folder levels:

EmailUtils = require('../app/utils/email-utils.js')(compound);

or

EmailUtils = require('../utils/email-utils.js')(compound);

or

EmailUtils = require('./utils/email-utils.js')(compound);

Could anyone help explain the issue and how to solve it. And how do we find the folder is being checked for require?


Solution

  • require('../utils/email-utils.js') should normally work, because that's the correct location.

    However, apparently CompoundJS does something funky, since it's running your code using eval, which makes the require relative to some path from within the CompoundJS module itself (reading the CompoundJS docs, it's because you're using eval controllers).

    I think it's best resolved by adjusting the NODE_PATH environment variable so it includes your base directory (compoundApp). That way, require('app/utils/email-utils') should work (although I'm not familiar with the intricacies of Node environment variables on Windows).