title: Unable to connect to MongoDB using node.js
body: <p>I installed MongoDB in my machine and I'm able to start mongod and connect to the db using the mongo command. I even imported some data in the <code>mycol</code> collection of the <code>mydb</code> db:</p>
  
  
  
  <pre><code>$ sudo start mongodb
  mongodb start/running, process 31008
  $ mongo
  MongoDB shell version: 2.4.9
  connecting to: test
  &gt; use mydb
  switched to db mydb
  &gt; db.mycol.count();
  5730
  &gt; ^C
  bye
  </code></pre>
  
  <p>But now if I want to access the db via <code>node.js</code> it doesn't work anymore, I get a <code>TypeError</code>.</p>
  
  <p>I try this code (<strong>UPDATED</strong>):</p>
  
  <pre class="lang-js prettyprint-override"><code>var MongoClient = require('mongodb').MongoClient;
  
  MongoClient.connect('mongodb://localhost:27017/mydb', function(err, db) {
      if(err) throw err;
      console.log("connected!");
  });
  </code></pre>
  
  <p>This is based on the npm module <code>mongodb</code>, I also tried <code>mongoose</code> and <code>mongojs</code>, each time with the same result. As soon as I want to connect to the database, I get a <code>TypeError</code>. </p>
  
  
  
  <pre><code>$ node mngclient.js 
  Failed to load c++ bson extension, using pure JS version
  
  events.js:172
      throw TypeError('type must be a string');
            ^
  TypeError: type must be a string
      at TypeError (&lt;anonymous&gt;)
      at EventEmitter.once (events.js:172:11)
      at Server.Base._registerHandler (/home/odi/dev/mydb/node_modules/mongodb/lib/mongodb/connection/base.js:387:23)
      at null.&lt;anonymous&gt; (/home/odi/dev/mydb/node_modules/mongodb/lib/mongodb/connection/server.js:410:12)
      at EventEmitter.emit (events.js:91:17)
      at null.&lt;anonymous&gt; (/home/odi/dev/mydb/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:111:15)
      at EventEmitter.emit (events.js:97:17)
      at Socket.&lt;anonymous&gt; (/home/odi/dev/mydb/node_modules/mongodb/lib/mongodb/connection/connection.js:297:10)
      at Socket.EventEmitter.emit (events.js:116:20)
      at Object.afterConnect [as oncomplete] (net.js:848:10)
  </code></pre>
  
  <hr>
  
  <p><strong>UPDATE</strong>:</p>
  
  <ul>
  <li>The version of the <code>mongodb</code> npm package is 1.3.23</li>
  <li><code>npm outdated</code> shows no output, so I guess my packages are up-to-date</li>
  <li>Using <code>mongoose</code> with the following very simple code brings up the exact same error:</li>
  </ul>
  
  <pre class="lang-js prettyprint-override"><code>var mongoose = require('mongoose');
  mongoose.connect('mongodb://localhost/mydb');
  </code></pre>
  
  <hr>
  
  <p><strong>UPDATE II</strong>:</p>
  
  <ul>
  <li>I still couldn't resolve this issue on my machine</li>
  <li>I setup the whole project in a virtual machine using vagrant. It works like a charm.</li>
  </ul>