node.js cannot find module 'mongodb'

I am going through my first node.js project. I've installed mongodb, have a server.js file, and when I try to run it I get this 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:362:17)
    at require (module.js:378:17)

I am quite certain I have mongodb installed, I am new to unix coming from a C# windows background, but I think this is a path not being configured properly?

The error you are getting indicates that the NPM package for MongoDB is not correctly installed.

The fix here depends on how you plan to leverage NPM. The NPM package manager operates has two different modes of operation: local and global.

The first (and default) mode is "local".

If you go to the folder with server.js you will see a sub-folder named node_modules. Under that folder will be a mongodb folder. If that folder is not present, then the mongodb module is not installed on that path.

To correct this, cd to that folder and type npm install mongodb. When the process is done you should have the node_modules/mongodb folder available.

You can also install MongoDB package globally using npm install -g mongodb. This is useful if you are using lots of node.js command-line stuff, but less useful if you are deploying the whole thing.

Side Note: there is an evolving standard around package.json. The package.json is a standardized way of including all dependencies for a given module. This allows you to run npm update or npm install at the root of a project / package and effectively "pull in" all of the dependencies. This greatly simplifies the deployment process and the process of keeping your dependencies in-line.

After trying for some time to install it without success (since I'm new to mongo and node), I was missing the npm link step indeed. So just to summarize, I did this:

  1. npm install mongodb -g
  2. cd /path/to/my/app/folder
  3. npm link mongodb

With that in place, I could do this in my application file: require('mongodb').

Here are some references, in case you need them:

I am new to MongoDB. After spending hours of installing mongodb through npm, I finally got the picture. See, there are actually three "mongodb" you need to deal with (I am using OSX):

1. The driver used in NodeJS: That is: var mongo = require('/usr/local/lib/node_modules/mongodb'). Or, you may use "npm link" as mentioned by earlier post in order to avoid the long path. Command "npm install -g mongodb" installs the mongodb driver under /usr/local/lib (this is what "-g" means). This guy took hours and hours to install, don't know why, so be patient!

2. The mongodb utilities (i.e., the UNIX executable commands). You download them from http://www.mongodb.org/downloads. It contains the mongod command that allows you to start the mongodb database. So in my /usr/local/bin, I created a symbolic link, mongod, pointing to /usr/local/lib/node_modules/mongodb-osx-x86_64-2.6.7/bin/mongod, so I can execute mongod from anywhere.

3. The mongodb database pointed by /data/db. So under /data, I created a symbolic link, db, pointing to /Users/your_user_id/Database/mongodb (or anywhere you like to put it), in which mongodb is an empty directory you may create via mkdir.

Because you have installed the #2 mongodb above, so you can execute mongod at the command line, which will create and start the database under mongodb #3.

Because you have mongodb #1 installed by npm, so now you can require it in your NodeJS program.

Job done!

I got the same Problem and I solve by these steps.

1.Go to Your Project Dir.(cd your Project Path)

2.Run this command in command shell( install npm)

3.Run your Project (node app.js for example)

This Problem would have caused by one among the below reasons.

1) You have not installed mongodb module.

2) You have installed mongodb in global context but not linked to current application.

Solution

1) Traverse to application top directory and execute npm install mongodb , this will install mongodb module, and your project will automatically detect it.

or

2) Execute npm install mongodb -g to install mongo DB module globally and then Ttaverse to application top directory and link using command npm link mongodb command.

Usefull Links

http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/

https://npmjs.org/doc/link.html