Node.js, mongoose and mongodb pain to install :(

I am having trouble getting these 3 packages for installation and collaboration. Here are the steps I took:

  • Installed nodejs 0.6.3 based on instructions for Linux (I downloaded the tarball from the site, and not using the distribution in git): https://github.com/joyent/node/wiki/Installation
  • Installed npm using the onliner installation found here: http://npmjs.org/
  • Installed npm packages for mongodb, mongojs and mongoose. Everything seems to be installed as expected.
  • Created a small testing program and received 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) 

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

I tried to tweak this line of code to fit this and still failed:

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

Any idea what I can do wrong? To clarify, do I need to create every version of npm I downloaded or do npm?

TIA!

+4
source share
2 answers

mongodb\bson not a module, where did you get this example?

The 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 get the ObjectID function ObjectID you should not rely on mongodb , but on mongoose through:

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

Read the Mongoose documentation .

+4
source

You may have installed mongodb in the wrong directory for your project. One good way to avoid such problems is to use the package.json file.

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

 { "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 needed.

Then run 'npm install' from this directory. It will install all the dependencies for your application. From there, your application should work fine.

+8
source

Source: https://habr.com/ru/post/1383775/


All Articles