Error: Cannot find module 'mongodb'

0 votes

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?

Oct 15 in Node-js by kartik
• 35,720 points
327 views

1 answer to this question.

0 votes

Hello @kartik,

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.

answered Oct 15 by Niroj
• 77,920 points

Related Questions In Node-js

0 votes
1 answer

Error:Cannot find module 'xmlhttprequest'

Hello @kartik, XMLHttpRequest is a built-in object in web ...READ MORE

answered Sep 7 in Node-js by Niroj
• 77,920 points
218 views
0 votes
1 answer

Error: Cannot find module 'ejs'

Hello @kartik, Go up one level (above app.js) ...READ MORE

answered Sep 7 in Node-js by Niroj
• 77,920 points
633 views
0 votes
1 answer

Error:npm throws error without sudo

Hello @kartik, This looks like a permissions issue ...READ MORE

answered Jul 8 in Node-js by Niroj
• 77,920 points
212 views
0 votes
1 answer

How to write files in Node.js?

Hello @kartik, Currently there are three ways to ...READ MORE

answered Jul 8 in Node-js by Niroj
• 77,920 points
97 views
0 votes
1 answer

How to Install a local module using npm?

Hello @kartik, This is what worked for me: npm ...READ MORE

answered Jul 9 in Node-js by Niroj
• 77,920 points
103 views
0 votes
1 answer

How to run Gulp tasks sequentially one after the other?

Hello @kartik, By default, gulp runs tasks simultaneously, ...READ MORE

answered Jul 12 in Node-js by Niroj
• 77,920 points
65 views
0 votes
1 answer

How to set environment variables from within package.json?

Hello @kartik, Set the environment variable in the ...READ MORE

answered Jul 13 in Node-js by Niroj
• 77,920 points
2,803 views
0 votes
1 answer

Error:Nodejs cannot find installed module on Windows

Hello @kartik, Add an environment variable called NODE_PATH and set ...READ MORE

answered Jul 15 in Node-js by Niroj
• 77,920 points
382 views
0 votes
1 answer

Error:NPM global install “cannot find module”

Hello @kartik, I got this error Error: Cannot find ...READ MORE

answered Jul 16 in Node-js by Niroj
• 77,920 points
2,862 views