node.js cannot find module 'mongodb'

前端 未结 11 2057
天涯浪人
天涯浪人 2020-12-07 22:36

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
    thro         


        
相关标签:
11条回答
  • 2020-12-07 22:42

    May be you are trying to run other then project path.I have faced the same problem and got resolved by following few steps given below.

    1.) Go to your project directory using command shell "cd /project path".

    2.) Now Run your project.

    0 讨论(0)
  • 2020-12-07 22:45

    I encountered the same problem, but re-installing mongodb in the terminal (npm install mongodb) fixed the error. Also mongodb was not originally in the list of dependencies in package.json before re-installing, so it appears it needed to be added there as well.

    0 讨论(0)
  • 2020-12-07 22:46

    Error

    jamsheed-h110m-s2ph:~/project/jamsheed/meanStack/mean$ node server.js
    module.js:538
        throw err;
        ^
    Error: Cannot find module 'mongodb'
        at Function.Module._resolveFilename (module.js:536:15)
        at Function.Module._load (module.js:466:25)
        at Module.require (module.js:579:17)
        at require (internal/module.js:11:18)
        at Object.<anonymous> (/home/haseeb/project/jamsheed/meanStack/mean/server.js:3:13)
        at Module._compile (module.js:635:30)
        at Object.Module._extensions..js (module.js:646:10)
        at Module.load (module.js:554:32)
        at tryModuleLoad (module.js:497:12)
        at Function.Module._load (module.js:489:3)
    

    Solved

    npm install mongodb

    Edit package.json = "mongodb": "^3.0.1", change "mongodb": "3.0.1",

    0 讨论(0)
  • 2020-12-07 22:48

    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!

    0 讨论(0)
  • 2020-12-07 22:49

    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.

    0 讨论(0)
  • 2020-12-07 22:50

    My problem was that when I originally created my package.json file, I had made the "version" key correspond to a value of "1.0.0". When I changed that to version value to reflect the version I wanted to use under my "dependencies" key, I got rid of the error. There is a picture below of the potential fix. Notice how the "version" and "mongodb" under "dependencies" match.

    0 讨论(0)
提交回复
热议问题