3

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,

11
  • What is the path that this code is in? Commented Mar 5, 2013 at 11:09
  • 1
    How are you executing that code? Do other small Node.js apps work using require from the same location? That code won't work in a web page. Commented Mar 5, 2013 at 11:46
  • 1
    That code only runs when executed in NodeJs. It's not for use with web pages. You need to create an API/routes/etc. to access and show the data on a web page. Commented Mar 5, 2013 at 13:00
  • 1
    Sorry for pointing this out: But you have no idea how this works. Node is in some very abstract way similar to PHP. This means: node.js runs on the server side, like PHP. The restrictions in the browser are the same as when having a website written with any other server side technology. The only thing that node.js shares with the browser is the programming language. Commented Mar 5, 2013 at 13:05
  • 1
    Thank you for your responses. You were right @Hippo. Commented Mar 5, 2013 at 13:48

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.