mauricemach / zappa

Node development for the lazy.
zappajs.org
MIT License
951 stars 85 forks source link

requires/mongodb models w/ zappa? #85

Open eethann opened 12 years ago

eethann commented 12 years ago

Hey there,

Thanks for Zappa. As a node/express/coffeescript n00b I should probably know better than to just jump right in to such an abstracted development system...but it's just too much fun!

That said, I'm having issues with requires in Zappa, specifically trying to require a provider using mongodb. I'm basing my code on the blog example from the Blog rolling tutorial on howtonode. I figured out that the require needs to be within the zappa require, like so:

require('zappa') ->
  HechoProvider = require('hechoprovider-mongodb').HechoProvider;

When I run the app, however, I get the following error:

Error: Cannot find module 'mongodb'
    at Function._resolveFilename (module.js:326:11)
    at Function._load (module.js:271:25)
    at require (module.js:355:19)
    at Object.<anonymous> (/home/ethan/projects/learning/nodejs/zappa/app-zappa/provider-mongodb.js:1:72)
    at Module._compile (module.js:411:26)
    at Object..js (module.js:417:10)
    at Module.load (module.js:343:31)
    at Function._load (module.js:302:12)
    at require (module.js:355:19)
    at Object.<anonymous> (eval at <anonymous> (/home/ethan/node_modules/zappa/lib/zappa.js:35:12))

Where the provider-mongodb.js file starts with

var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
<--snip-->
exports.HechoProvider = HechoProvider;

Is there a better way to use mongodb providers with Zappa? Is there something I should be doing to get the above code working?

Thanks,

Ethan

mauricemach commented 12 years ago

You're a brave man! :)

Sorry, require in zappa 0.2.x is problematic because it's not the real deal. This is why we're moving to a @-based API in 0.3.0 (whoa, déjà vu).

Until 0.3.0 is released, you can use this if you're having problems:

require('zappa') {require}, ->
  HechoProvider = require('hechoprovider-mongodb').HechoProvider

Which is just a way to pass the real require to the zappa block, equivalent to the javascript:

require('zappa')({require: require}, function(){
  var HechoProvider = require('hechoprovider-mongodb').HechoProvider;
});

As a side note, the following CoffeeScript lines are equivalent:

  HechoProvider = require('hechoprovider-mongodb').HechoProvider
  {HechoProvider} = require 'hechoprovider-mongodb'
eethann commented 12 years ago

Thanks for the amazingly quick response and the pointers...and the bit of mindbending coffeescript lesson at the end.

I added that line to the top of the app file, and while it is including just dandy now, it is still not finding the mongodb module, though the module is found in the basic Express app.

I'm definitely getting the sense that I may be a bit out of my depth here...but I'm game to keep on going. Any other ideas?

mauricemach commented 12 years ago

Is the express app in the exact same dir as the zappa one?

Also, in the same file where the zappa app is, if you take zappa out of the equation, does it work?

Ex.:

console.log require('hechoprovider-mongodb').HechoProvider
eethann commented 12 years ago

Doh. Just learned where npm modules are installed in Express projects.

Thanks.

Ethan On Sep 20, 2011 3:16 PM, "Maurice Machado" < reply@reply.github.com> wrote:

Is the express app in the exact same dir as the zappa one?

Also, in the same file where the zappa app is, if you take zappa out of the equation, does it work?

Ex.:

console.log require('hechoprovider-mongodb').HechoProvider

Reply to this email directly or view it on GitHub: https://github.com/mauricemach/zappa/issues/85#issuecomment-2148708