Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...