mongodb Client + Node.js

I followed the "Basic Introduction to MongoDB" from (http://mongodb.github.com/node-mongodb-native/api-articles/nodekoarticle1.html)

I installed node-v0.8.21 from sources to this directory "/home/myuser/lib/node/" (I'am not root on the machine)

I set the proxy for npm and launched this command to install "mongodb" driver: "./npm install mongodb" The command returned in success and produced a mongodb directory in "/home/myuser/lib/node/bin/node_modules/".

I don't know how to use the driver now.. I tried this:

// Retrieve
var MongoClient = require('mongodb').MongoClient;

// Connect to the db
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
if(!err) {
 console.log("We are connected");
}
});

But I always have this error code: "Uncaught ReferenceError: require is not defined"

Thanks in advance,

You are trying to run server JavaScript code on browser. JavaScript is no longer a client side scripting language. nodeJS uses JavaScript to run a server framework, and is getting popular by the day.

I don't know how to use the driver now.. I tried this:

People not familiar with nodeJS make that mistake. You have to understand that nodeJS is like any other server serving HTML pages. At the server you have server-side script, that executes, and client-side content that the server delivers. Only that JavaScript executes both at server and client in nodeJS. You should learn how to use node before you can learn using mongodb package. Here are some links:

  1. How do I get started with Node.js
  2. What is node.js?

To test file in nodeJS

  1. You create a file say app.js and put the code snippet you gave inside it. app.js should be immideately inside the folder where you did ./npm install mongodb
  2. Then from the same location run the app, by doing node app.js