ReferenceError: require is not defined in MongoDB wrapper

I am trying to connect MongoDB from Mongo client to windows command (Window 8.1). When i use require()

in javascript i have an error as below. Does anyone have the same problem? Am I missing the npm install require

? How does MongoDB wrapper not find the function require



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



source to share

2 answers

You are confusing the administrative shell mongo

with Node.js . Although both environments use JavaScript, the wrapper mongo

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 apps using MongoDB driver (as per your example code) you need to use an interpreter node

. The Node.js driver documentation includes a quick start guide with examples to help you get started.



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



All Articles