node.js cannot find module 'mongodb'

前端 未结 11 2076
天涯浪人
天涯浪人 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:59

    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.

提交回复
热议问题