occasl
occasl

Reputation: 5454

node.js, mongoose and mongodb a pain to install :(

I'm having trouble getting these 3 packages to install and work together. Here are the steps I took:

  1. Installed nodejs 0.6.3 based on the instructions here for Linux (I downloaded the tar ball from the site as opposed to using the distro in git): https://github.com/joyent/node/wiki/Installation
  2. Installed npm using the onliner install found here: http://npmjs.org/
  3. Installed npm packages for mongodb, mongojs and mongoose. All seem to install as expected.
  4. Created a small program to test and get the following exception:

    Error: Cannot find module 'mongodb/bson'
    at Function._resolveFilename (module.js:334:11)
    at Function._load (module.js:279:25)
    at Module.require (module.js:357:17)
    at require (module.js:368:17)
    at Object.<anonymous> (/local/mnt/apps/node-v0.6.3/app.js:6:16)
    at Module._compile (module.js:432:26)
    at Object..js (module.js:450:10)
    at Module.load (module.js:351:31)
    at Function._load (module.js:310:12)
    at Array.0 (module.js:470:10)
    

bson.js appears under this directory for me: /opt/node/node_modules/mongodb/lib/mongodb/bson

I've tried adjusting this line of code to match that and still no success:

var mongoose = require('mongoose').Mongoose,
ObjectID = require('mongodb/bson').ObjectID;

Any idea what I might be doing wrong? Just to clarify, do I need to build each npm install I downloaded or does npm do that?

TIA!

Upvotes: 4

Views: 7514

Answers (2)

Jan Jongboom
Jan Jongboom

Reputation: 27323

mongodb\bson is no module, where did you get this example from?

Normal use of mongo in node.js is achieved by:

var mongoose = require('mongoose');
var mongodb = require('mongodb');

Now you can connect via

mongoose.connect("url");

When trying to retrieve the ObjectID function you shouldn't rely on mongodb but on mongoose via:

var schema = mongoose.Schema,
    objectId = schema.ObjectId;

Please read the Mongoose documentation.

Upvotes: 4

ced
ced

Reputation: 1928

It's possible that you installed mongodb in the wrong directory for your project. One good way to avoid these sorts of issues is to use a package.json file.

Create a directory for your node project and move your .js file into it. Add a file called package.json with these contents:

{  
   "name": "application-name",
   "private": true,
   "dependencies": {
      "mongodb": ">=0.9.6-7",
      "mongoose": ">=0.0.1"
    }
}

You can follow the pattern to add your other dependencies as necessary.

Then from that directory, run 'npm install'. It will install all dependencies for your app. From there your app should run fine.

Upvotes: 8

Related Questions