node.js cannot find module

2126 ワード

初めてnodejsに接触すると以下の問題が発生します.
module.js:340
    throw err;
         ^Error: Cannot find module 'mongodb'
解決方法は以下の通りです.
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 inded.So just to summare、I did this:
  • npm install mongodb-g
  • cd/path/to/my/ap/folder
  • npm link mongodb
  • With that in place,I could do this in my appication file:require('mongodb')
    Here are some references,in case you need them:
  • npm global vs local
  • npm link
  • Introduction to Mongo DB
  • ケース:
    I am new to MongoDB.After spending hors 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 mogo=require('/usr/local/lib/nodemumodules/momogodb').Or,Youmay use“npm link”as mentioned byearlininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininitititititititititititititititititititittrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrdedededededededededededededewhat「-g」means.Thisgy took houss and houss to install,don't know why、so be patient!
    2.The mongodb utilities(i.e.the UNIX executable commands).You download themfromhttp://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 smbonic link,monogod,poingto/usr/local/lib/nodemodules/mogodb-osx-x 86_64-2.67/bin/mongod、so I can execute mongod from anywhere.
    3.The mongodb database pointed by/data/db.So under/data、I created a smbonic link、db、poingto/Users/your_アメリカ.id/Database/mongodb(or anywhere you like to put it)、in which mogodb is an empty directory you may create via mkdir.
    Because you have installed the落2 mongodb aboove、so you can execute mongod at the command line、which will create and start the database under mongodb
    Because you have mongodb铅1 installed by npm,so now you can require it in your NodeJS program.
    Job done