1

Using Passport JS with Mojito

I am trying to use passportjs with Mojito by doing the following:

1) after installing Node JS and Mojito

2) created a mojito app

3) created a mojit

4) included passport as a dependency in the package.json of the Mojito app

5) ran npm install (it fetched passport into the node_modules directory, which is in the app dir)

6) in the function for the init action of the controller (in controller.server.js of the mojit created in step 3), I am requiring the passport module (this.passport = require('passport')), however Mojito complains with the following error:

/usr/local/lib/node_modules/mojito/node_modules/yui/yui-nodejs/yui-nodejs.js:1147 throw (e || new Error(msg)); ^ Error: Cannot find module 'passport'

I've tried adding passport to the requires array that is passed to the YUI.add function in controller.server.js and according to the logs the mojito dispatcher is dispatching an instance of the my mojit/index with the passport module, however it also warns: [YUI-WARN] yui: NOT loaded: passport

Any ideas of what I might be doing wrong or any example of using any non-YUI nodejs module in Mojito?

Thanks!

by
  • BL
  • Sep 12, 2012
8 Replies
  • Try moving passport.js to /autoload of your app.

    0
  • Thanks for the suggestion, however there is no single JS file that I could move to the autoload folder, given that PassportJS is a Node JS module (i.e. installed through npm into the node_modules dir, under the app dir); therefore I copied the entire dir (passport from node_modules) to <app_dir>/autoload. When trying to start mojito from the app dir, I now get the following:

    [ERROR] (1347488397063) addon-rs-yui: module is not defined
    ReferenceError: module is not defined
    at <app_dir>/autoload/passport/lib/passport/context/http/actions.js:4:15
    at RSAddonYUI._captureYUIModuleDetails (<app_dir>/node_modules/mojito/lib/app/addons/rs/yui.server.js:626:23)
        at RSAddonYUI.parseResourceVersion (<app_dir>/node_modules/mojito/lib/app/addons/rs/yui.server.js:223:22)
        at ResourceStore.<anonymous> (/usr/local/lib/node_modules/mojito/node_modules/yui/oop/oop-min.js:7:2282)
        at [object Object].exec (/usr/local/lib/node_modules/mojito/node_modules/yui/event-custom-base/event-custom-base-min.js:7:1138)
        at ResourceStore.parseResourceVersion (/usr/local/lib/node_modules/mojito/node_modules/yui/event-custom-base/event-custom-base-min.js:7:535)
        at /usr/local/lib/node_modules/mojito/lib/store.server.js:1787:30
        at ResourceStore._walkDirRecursive (/usr/local/lib/node_modules/mojito/lib/store.server.js:1875:21)
        at ResourceStore._walkDirRecursive (/usr/local/lib/node_modules/mojito/lib/store.server.js:1878:30)
        at ResourceStore._walkDirRecursive (/usr/local/lib/node_modules/mojito/lib/store.server.js:1878:30)
    ✖ There was an error starting the application:

    ✖ Cannot read property 'name' of undefined
        TypeError: Cannot read property 'name' of undefined
        at RSAddonYUI.parseResourceVersion (<app_dir>/node_modules/mojito/lib/app/addons/rs/yui.server.js:224:35)
        at ResourceStore.<anonymous> (/usr/local/lib/node_modules/mojito/node_modules/yui/oop/oop-min.js:7:2282)
        at [object Object].exec (/usr/local/lib/node_modules/mojito/node_modules/yui/event-custom-base/event-custom-base-min.js:7:1138)
        at ResourceStore.parseResourceVersion (/usr/local/lib/node_modules/mojito/node_modules/yui/event-custom-base/event-custom-base-min.js:7:535)
        at /usr/local/lib/node_modules/mojito/lib/store.server.js:1787:30
        at ResourceStore._walkDirRecursive (/usr/local/lib/node_modules/mojito/lib/store.server.js:1875:21)
        at ResourceStore._walkDirRecursive (/usr/local/lib/node_modules/mojito/lib/store.server.js:1878:30)
        at ResourceStore._walkDirRecursive (/usr/local/lib/node_modules/mojito/lib/store.server.js:1878:30)
        at ResourceStore._walkDirRecursive (/usr/local/lib/node_modules/mojito/lib/store.server.js:1878:30)
        at ResourceStore._walkDirRecursive (/usr/local/lib/node_modules/mojito/lib/store.server.js:1878:30)


    ✖ Mojito was not started!


    Is Mojito expecting YUI-only modules in the autoload dir? Is there an example of using any non-YUI Node JS module (installed through npm) inside a Mojito app?

    Thanks for your time!



    QUOTE(Ren @ 12 Sep 2012 1:22 PM)
    Try moving passport.js to /autoload of your app.

    0
  • Sorry my answer was off the mark, I didn't realize it was a NPM module.
    0
  • Hi BL,

    I just created an app, locally installed 'passport', and was able to access it in the controller with 'require("passport")'.
    I was using Mojito 0.4.3.

    Here are the steps that I followed in a terminal shell:

    1. $ mojito create app myApp
    2. $ cd myApp
    3. $ mojito create mojit myMojit
    4. $ npm install passport
    5. $ cd mojits/myMojit
    6. $ vi controller.server.js
        
       Y.namespace('mojito.controllers')[NAME] = {
            init: function(config) {
                this.passport = require('passport');
                this.config = config;
            },
            index: function(ac) {
                console.log(this.passport);
                 ...
            ....

    7. $ mojito start
    8. Output in terminal:
     
      { _key: 'passport',
        _strategies: { session: { name: 'session' } },
        _serializers: [],
        _deserializers: [],
        _infoTransformers: [],
        _userProperty: 'user',
        version: '0.1.12',
        Passport: [Function: Passport],
        Strategy: [Function: Strategy],
        strategies: { SessionStrategy: { [Function: SessionStrategy] super_: [Function: Strategy] } } }


    Have you checked to see if Node.js can find the package? From the Mojito application directory where 'passport' is installed in 'node_modules', try running the following: $ node -pe 'require.resolve("passport")'

    If the above command outputs the error message below, then Node.js can't find the package (not in $NODE_PATH), thus, Mojito
    will not be able to find it either.
        
    throw new Error("Cannot find module '" + request + "'");

    The solution would be add "./node_modules" to NODE_PATH in .bashrc or from the command-line. If that's not the problem, let's take a look at the version of Mojito you're using ($ mojito version) and pick up from there. 

    Thanks.

    0
  • Thanks a lot for the detailed response, however I am still encountering the same issue.

    I uninstalled everything and started from scratch, following the same steps as you outlined.

    I am currently running: node v0.6.13 and Mojito v0.4.4 (and passport v0.1.12)

    The output of the node -pe 'require.resolve("passport")' command, run from my Mojito app dir is as expected: <myApp_dir>/node_modules/passport/lib/passport/index.js
    however after I start Mojito from the app dir and navigate to the localhost:8666 the same "Cannot find module 'passport'" error is encountered.

    Here's my config and code:

    --> routes.json
    [{
        "settings": [ "master" ],
        "home": {
            "verbs": ["get"],
            "path": "/",
            "call": "@myMojit.index"
        }
    }]

    --> myMojit controller.server.js
    YUI.add('myMojit', function(Y, NAME) {

    /**
     * The myMojit module.
     *
     * @module myMojit
     */

        /**
         * Constructor for the Controller class.
         *
         * @class Controller
         * @constructor
         */
        Y.namespace('mojito.controllers')[NAME] = {

            init: function(config) {
                this.passport = require('passport');
                this.config = config;
            },

            /**
             * Method corresponding to the 'index' action.
             *
             * @param ac {Object} The ActionContext that provides access
             *        to the Mojito API.
             */
            index: function(ac) {
                console.log(this.passport);
                ac.models.myMojitModelFoo.getData(function(err, data) {
                    if (err) {
                        ac.error(err);
                        return;
                    }
                    ac.assets.addCss('./index.css');
                    ac.done({
                        status: 'Mojito is working.',
                        data: data
                    });
                });
            }

        };

    }, '0.0.1', {requires: ['mojito', 'myMojitModelFoo']});


    Appreciate your help and time!


    QUOTE(Me @ 12 Sep 2012 4:45 PM)
    Hi BL,

    I just created an app, locally installed 'passport', and was able to access it in the controller with 'require("passport")'.
    I was using Mojito 0.4.3.

    Here are the steps that I followed in a terminal shell:

    1. $ mojito create app myApp
    2. $ cd myApp
    3. $ mojito create mojit myMojit
    4. $ npm install passport
    5. $ cd mojits/myMojit
    6. $ vi controller.server.js
        
       Y.namespace('mojito.controllers')[NAME] = {
            init: function(config) {
                this.passport = require('passport');
                this.config = config;
            },
            index: function(ac) {
                console.log(this.passport);
                 ...
            ....

    7. $ mojito start
    8. Output in terminal:
     
      { _key: 'passport',
        _strategies: { session: { name: 'session' } },
        _serializers: [],
        _deserializers: [],
        _infoTransformers: [],
        _userProperty: 'user',
        version: '0.1.12',
        Passport: [Function: Passport],
        Strategy: [Function: Strategy],
        strategies: { SessionStrategy: { [Function: SessionStrategy] super_: [Function: Strategy] } } }


    Have you checked to see if Node.js can find the package? From the Mojito application directory where 'passport' is installed in 'node_modules', try running the following: $ node -pe 'require.resolve("passport")'

    If the above command outputs the error message below, then Node.js can't find the package (not in $NODE_PATH), thus, Mojito
    will not be able to find it either.
        
    throw new Error("Cannot find module '" + request + "'");

    The solution would be add "./node_modules" to NODE_PATH in .bashrc or from the command-line. If that's not the problem, let's take a look at the version of Mojito you're using ($ mojito version) and pick up from there. 

    Thanks.

    0
  • > what is the recommended version of Node JS for Mojito is?
    > The dependency says anything from 0.4 to less than 0.7; but is there a recommended/most used/proven Node JS version for Mojito?

    We routinely run Mojito on Node.js 0.8, latest version.
    I've filed https://github.com/yahoo/mojito/issues/496 to address this.





    0
  • Hi BL,

    Sure, sorry for the frustration you may be feeling. I did some tests and think it has to do with NODE_PATH not being set.

    For example:
    // Setting NODE_PATH=''

    $ export NODE_PATH=''
    $ node -pe 'require.resolve("passport")'
    # /Users/jcatera/mojito/moj_test_apps/joe/node_modules/passport/lib/passport/index.js 
    $ mojito start
    # Error: Cannot find module 'passport'


    // Setting NODE_PATH="./node_modules:/opt/local/lib/node:/opt/local/lib/node_modules"

    $ export NODE_PATH="./node_modules:/opt/local/lib/node:/opt/local/lib/node_modules"
    $ node -pe 'require.resolve("passport")'
    # /Users/jcatera/mojito/moj_test_apps/joe/node_modules/passport/lib/passport/index.js
    $ mojito start
    # No error

    If you haven't already (I'm guessing this is the problem), set NODE_PATH in .bashrc with the following and then source the file:

    export NODE_PATH="./node_modules:/opt/local/lib/node:/opt/local/lib/node_modules"

    The way Node.js resolves modules and uses NODE_PATH can be found in the Node.js API docs:

      - http://nodejs.org/api/modules.html#modules_all_together

    This blog has a nice explanation of Node.js looks for modules:

      - http://www.bennadel.com/blog/2169-Where-Does-Node-js-And-Require-Look-For-Modules-.htm

    Hope that helps. If not, I'll have a colleague try to help you.


    0
  • Thanks Joe! Indeed setting NODE_PATH as you indicated solves the issue.
    0

Recent Posts

in Yahoo! Mojito