0

TL;DR: Following is a detailed explanation of the steps I followed to arrive at the error. To find the basic question, skip to the very bottom.


I'm following this book addyosmani - backbone-fundamentals for creating a simple backbone.js app with node server and mongodb as backend.

As instructed, I've installed the latest versions of Node.js fromnodejs.org and mongodb from www.mongodb.org and ran mongodb by followed the instructions.

Now as per the tutorial, I've created the following:

{ //package.json
  "name": "backbone-library",
  "version": "0.0.1",
  "description": "A simple library application using Backbone",
  "dependencies": {
    "express": "~3.1.0",
    "path": "~0.4.9",
    "mongoose": "~3.5.5"
  }
}

at the root of my project and executed npm install, for some reason it didn't work out.

I further ventured and installed express following these steps:

npm init
npm install express --save
npm install express

this created the following package.json which I believe to be essentially the same as the one mentioned in tutorial:

{
  "name": "Backbone-Library",
  "version": "0.0.1",
  "description": "A simple library application using Backbone",
  "main": "index.html",
  "dependencies": {
    "express": "^3.18.3",
    "mongoose": "^3.5.16",
    "path": "^0.4.9"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

This created the very same folder structure which is mentioned in the tutorial:

node_modules
|_ .bin
|_ express
|_ mongoose
|_ path

and then I've successfully executed npm install without any errors.


Now once I create a simple server.js file and try to execute it using the command node server.js I get the following error:

module.js:340
throw err;
      ^
Error: Cannot find module 'mongodb'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (D:\<-- path to project ->\Backbone-Library\node_modules\mongoose\lib\utils.js:5:16)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)

(I can provide the full trace if needed).

I've found these StackOverflow posts regarding the same error, and the answers says that I've to install mongodb again using npm.

I've got confused because the book doesn't mention anything regarding this step.

I'm pretty new to everything I've mentioned here except backbone - node.js, npm, mongodb etc so I might be missing some basic concepts.

I'd like to know if it is possible to use the existing db which I installed previously rather than downloading and installing it again via npm.

If not, why is it required to install it again using npm, what is the difference between original installation and npm installation..?

Community
  • 1
  • 1
T J
  • 42,762
  • 13
  • 83
  • 138

2 Answers2

1

The original install installed the db itself on your machine. The npm install installs the package node.js needs to communicate with mongodb. NPM is like NuGet for .NET. You have a database, but still have to install the package for the framework to communicate to the database itself

Here is a sample on how to use mongodb module to connect to a mongo instance in node.js

var MongoClient = require('mongodb').MongoClient;

    MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
        if(err) throw err;

        var query = { 'grade' : 100 };

        db.collection('grades').find(query).toArray(function(err, docs) {
            if(err) throw err;

            console.dir(docs);

            db.close();
        });
    });
blackmind
  • 1,286
  • 14
  • 27
  • So, `npm install mongodb` isn't installing the db itself..? – T J Nov 25 '14 at 16:57
  • 1
    no it installs a package/module for node.js to know how to use mongodb and communicate with it. Think of it has a driver for node.js to work with mongodb – blackmind Nov 25 '14 at 16:58
1

MongoDB is a database software; similar to how MySQL is a software. Oftentimes your interactions with a database layer are managed by some sort of abstract library - in this case, the node module mongoose.

It sounds like you have require('mongodb'), when you should have require('mongoose'). This will bring in the Mongoose module, which is essentially a code library for interacting with your MongoDB database.

It sounds like you're confused a bit on terminology. If you have any questions, feel free to hit me up :)

Jordan Foreman
  • 3,848
  • 7
  • 40
  • 65
  • 2
    that's if he wants to use mongoose. He can still use mongodb package – blackmind Nov 25 '14 at 16:59
  • the `server.js` file is exactly as shown here http://addyosmani.github.io/backbone-fundamentals/#create-a-simple-web-server and it has `require('mongoose')` ... – T J Nov 25 '14 at 17:01
  • Do you have MongoDB installed on your computer? – Jordan Foreman Nov 25 '14 at 17:01
  • Could you put your full stack trace in a pastebin or something...I think that would help me out a bit – Jordan Foreman Nov 25 '14 at 17:02
  • @JordanForeman Of course I'm confused with terminology or the basic usage of `npm`. `mongoose` has listed `mongodb` as their dependency in it's `package.json` does this mean the `mongoose` folder should in turn have another `node_modules` folder inside it which contains `mongodb`..? Is`npm install` in the root folder of my project supposed to install all listed dependencies, their dependencies, dependencies of their dependencies and so on..? – T J Nov 25 '14 at 17:17
  • You're correct that installing any package should automagically install its dependencies recursively. MongoDB is a different kind of dependency being that it isn't (by definition) an NPM module. It sounds like one exists, but you're probably better served installing it standalone. The more I read your comments, the less I believe my answer is going to be helpful. It sounds like either your connection logic is broken, or some child dependencies aren't installing as expected... – Jordan Foreman Nov 25 '14 at 17:21