Search code examples
javascriptnode.jsmoduleyuimojito

Mojito - Load NodeJS Module


I'm using :

  • Mojito 0.5.5
  • NPM 1.2.14
  • NodeJS 0.8.22

I'm trying to create a simple Mojito application using NodeJS modules, packaged with NPM.

Documentation say that :

  • Create a Mojito application.
  • Add any needed dependencies to dependencies object in package.json.
  • Install dependencies with npm: {app_dir}$ npm install
  • When Mojito starts, your application will have access to the installed npm modules.

Indeed, the example above isn't working.

"dependencies": {
    "mojito": "0.5.x",
    "async": "0.2.x"
},

status: function(ac) {
    var async = require('async');
    ac.done({
        status: 'loaded'
    });
}

Mojito server : "Error: Cannot find module 'async'"

Web page error : "Error: MODULE_NOT_FOUND"

Any clue ?

PS : Of course, after npm install, the project folder have a node_modules folder with the async package.


Solution

  • As today, mojito npm package is used as a cli but it also holds the mojito core. This is problematic because most developers will run the app by doing mojito start, which runs from the global mojito package. This, plus the fact that the current YUI is messing with the require, it causes a lot of troubles when requiring dependencies.

    The solution is simple, make sure you install mojito locally in your application, and run the app by doing ./node_modules/mojito/bin/mojito start or by doing node server.js instead of mojito start.

    Aside from that, we are working on splitting the cli and core into separate packages, and waiting for YUI 3.9.0 to solve this problem once for all so developers can keep using mojito start.