5

I try to connect MongoDB from Mongo client on windows command (Window 8.1). When I use require() in javascript, I have error as below. Does any one has same issue? Did I miss any require related npm installation? How can't MongoDB shell find require function?

C:\tutorial\nodeMongoAngular-master\lesson2>mongo

MongoDB shell version: 3.0.1
connecting to: test
var MongoClient = require('mongodb').MongoClient;

2015-04-30T14:33:25.812-0400 E QUERY    ReferenceError: require is not defined
    at (shell):1:19
Stennie
  • 63,885
  • 14
  • 149
  • 175
Scott Lee
  • 51
  • 1
  • 1
  • 5

2 Answers2

5

You are confusing the mongo administrative shell with the Node.js driver. While both environments happen to use JavaScript, the mongo shell has more limited I/O support and is not intended to be used as a driver for application development.

If you want to write Node.js applications using the MongoDB driver (as per your example code), you need to use the node interpreter. The Node.js driver documentation includes a Quickstart tutorial with examples that should help you get started.

Stennie
  • 63,885
  • 14
  • 149
  • 175
1

@Scott Lee: if you're still looking for an answer, try running with command "node yourscript.js". Make sure mongod is running. 'mongo script.js' will work for pure mongo scripts without nodejs code.

Shona
  • 261
  • 1
  • 4
  • 15