简体   繁体   English

node.js,mongoose和mongodb安装很痛:(

[英]node.js, mongoose and mongodb a pain to install :(

I'm having trouble getting these 3 packages to install and work together. 我无法安装这3个软件包并一起工作。 Here are the steps I took: 以下是我采取的步骤:

  1. Installed nodejs 0.6.3 based on the instructions here for Linux (I downloaded the tar ball from the site as opposed to using the distro in git): https://github.com/joyent/node/wiki/Installation 根据这里针对Linux的说明安装了nodejs 0.6.3(我从网站下载了tar球而不是在git中使用发行版): https//github.com/joyent/node/wiki/Installation
  2. Installed npm using the onliner install found here: http://npmjs.org/ 使用在线安装程序安装npm: http ://npmjs.org/
  3. Installed npm packages for mongodb, mongojs and mongoose. 为mongodb,mongojs和mongoose安装了npm包。 All seem to install as expected. 所有似乎都按预期安装。
  4. Created a small program to test and get the following exception: 创建了一个小程序来测试并获得以下异常:

     Error: Cannot find module 'mongodb/bson' at Function._resolveFilename (module.js:334:11) at Function._load (module.js:279:25) at Module.require (module.js:357:17) at require (module.js:368:17) at Object.<anonymous> (/local/mnt/apps/node-v0.6.3/app.js:6:16) at Module._compile (module.js:432:26) at Object..js (module.js:450:10) at Module.load (module.js:351:31) at Function._load (module.js:310:12) at Array.0 (module.js:470:10) 

bson.js appears under this directory for me: /opt/node/node_modules/mongodb/lib/mongodb/bson bson.js出现在我的目录下:/ opt / node / node_modules / mongodb / lib / mongodb / bson

I've tried adjusting this line of code to match that and still no success: 我已经尝试调整这行代码来匹配,但仍然没有成功:

var mongoose = require('mongoose').Mongoose,
ObjectID = require('mongodb/bson').ObjectID;

Any idea what I might be doing wrong? 知道我可能做错了什么吗? Just to clarify, do I need to build each npm install I downloaded or does npm do that? 只是为了澄清一下,我是否需要构建我下载的每个npm安装或者npm这样做?

TIA! TIA!

It's possible that you installed mongodb in the wrong directory for your project. 您可能已将mongodb安装在项目的错误目录中。 One good way to avoid these sorts of issues is to use a package.json file. 避免这些问题的一个好方法是使用package.json文件。

Create a directory for your node project and move your .js file into it. 为节点项目创建一个目录,并将.js文件移动到该目录中。 Add a file called package.json with these contents: 添加一个名为package.json的文件,其中包含以下内容:

{  
   "name": "application-name",
   "private": true,
   "dependencies": {
      "mongodb": ">=0.9.6-7",
      "mongoose": ">=0.0.1"
    }
}

You can follow the pattern to add your other dependencies as necessary. 您可以按照模式添加其他依赖项。

Then from that directory, run 'npm install'. 然后从该目录运行'npm install'。 It will install all dependencies for your app. 它将为您的应用安装所有依赖项。 From there your app should run fine. 从那里你的应用程序应该运行正常。

mongodb\\bson is no module, where did you get this example from? mongodb\\bson是没有模块的,你从哪里得到这个例子?

Normal use of mongo in node.js is achieved by: 在node.js中正常使用mongo是通过以下方式实现的:

var mongoose = require('mongoose');
var mongodb = require('mongodb');

Now you can connect via 现在你可以通过连接

mongoose.connect("url");

When trying to retrieve the ObjectID function you shouldn't rely on mongodb but on mongoose via: 当试图检索ObjectID功能,您不应该依赖mongodb ,但是, mongoose通过:

var schema = mongoose.Schema,
    objectId = schema.ObjectId;

Please read the Mongoose documentation . 请阅读Mongoose文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM