node.js cannot find module 'mongodb'
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
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:
- npm install mongodb -g
- cd /path/to/my/app/folder
- 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:
- npm global vs local
- npm link
- Introduction to Mongo DB
You can do either of two things to make it run :-
1) Install mongodb globally with below steps :-
a)npm install mongodb -g
b) Go to your app directory, where module.js is located and then run
npm link mongodb
Explanation :- When you install a package globally via npm, it is downloaded to global node_module folder. For me(Mac user), it's under /usr/local/lib/node_modules/mongodb. We link this to that directory from where you are trying to run module.js.
2) Another approach is to install mongodb locally, not globally via
npm install mongodb
After following either of these, you will be seeing node_modules --> mongodb folder under the 'module.js' directory, which means mongodb has been successfully installed.
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.