javascript代码实例教程-node.js cannot find module

发布时间:2019-01-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-node.js cannot find module脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 次接触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 indeed. So just to summarize, I did this:

 

npm install mongodb -g

cd /path/to/my/app/folder

npm link mongodb

With that in place, I could do this in my application 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 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 Fromhttps://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!

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-node.js cannot find module全部内容,希望文章能够帮你解决javascript代码实例教程-node.js cannot find module所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。