Search code examples
javascriptnode.jsmongodbbackbone.js

Unable to find module mongodb in simple node.js app


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..?


Solution

  • 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();
            });
        });