2

I am trying to connect to mongoDB from node.js and upload a file("functions") to MongoDB. Can someone please verify whats the issue with my code is.

When I run the js file, I am getting following error: Error: Cannot find module 'mongodb' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25)

Code is as follows:

  var mongodb = require('mongodb');
   var url = require('url');
   var log = console.log;
   var currentTimeStamp = new Date();
   var file = require (__dirname + '/functions');

mongodb.MongoClient.connect('mongodb://phx8b03c-fb1d-6.stratus.phx.ebay.com,phx8b03c-316d-6.stratus.phx.ebay.com,phx8b03c-9564-6.stratus.phx.ebay.com',
    function (err, client) {
        if (err) throw err;

        client.createCollection('lbTopology' , function (err, collection) {
            if (err) throw err;

            collection.insert(file, 'lbTopology' , function (err) {
                if (err) throw err;

                client.close(function (err) {
                    if (err) throw err;

                    console.log('done');
                });
            });
        });
    }); 

Can someone please let me know what the issue is? Thanks a lot in advance

1 Answer 1

2

It looks like you don't have mongodb installed. Did you npm install mongodb in the same directory with your code or do you have a node_modules folder with mongodb in it?

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I did a "npm install mongodb" and the error I get is -bash: npm: command not found . DO I need to install anything else before installing mongodb?
How did you install node? I prefer to use nvm (Node Version Manager). You should have the npm (Node Package Manger) installed so you can easily download modules (like mongodb) to use for your projects.

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.