My app throws exeception for mongodb module not found.

October 24, 2016 111 views
Node.js MongoDB Ubuntu 16.04

Hello Team,

I have application deployed on Ubuntu 16.04 with node js, mongodb, mongoose & express js. Until y'day my app was up and running with no issues. When I pushed the updates to server and restarted the server using pm2 it started giving below error :


Error: Cannot find module 'mongodb'
at Function.Module.resolveFilename (module.js:325:15)
at Function.Module.
load (module.js:276:25)
at Function.load (/usr/local/lib/nodemodules/pm2/nodemodules/pmx/lib/transaction.js:62:21)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/sumit/www/bulawaa/node
modules/mongoose/lib/drivers/node-mongodb-native/binary.js:6:14)
at Module.compile (module.js:409:26)
at Object.Module.
extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)

I have mongoose npm package installed, trying to figure out what changed in mongodb drivers that is causing this issue. Due to this my app is down.

Can some one help me get to the cause. Look forward to hear from you.

Kind Regards,
Sumit

1 Answer
xMudrii October 24, 2016
Accepted Answer

What about trying to update all npm packages you have with npm install?
You should execute it in directory where packages.json is located.

Didn't you by mistake called somewhere in code require('monogodb') instead ofrequire('mongoose')if you are usingmongoose` driver. Maybe a simple grep command can help you there:

  • grep -r "require('monogodb')" .

In last resort maybe you can try removing all node modules (node_modules), clearing out npm cache (npm cache clear) and install all modules again (npm install)

  • @xMudrll if I spell your name correctly. Thank you so much. I am still not sure what did go wrong but reinstalling all the packages with npm cache clear made the trick. Thank you so much for such quick turn around. You saved my time.

    I had another question if its ok here to answer. I have one server but I have configured three URLs. I want to keep three different application on different url on same droplet can I do that. Like would like to TEST, Stage and PROD. Is it possible to do that.

    Can you point to some good reference links within digital ocean.

    • Yes you can keep three apps on same Droplet as they are on different URLs.

      You can use pm2 to do it, but you must care about ports. Two node apps can't be assigned to same port.
      When you would try to run two node apps on port 8080, first would work, but second will return error.

      How To Set Up a Node.js Application for Production on Ubuntu 16.04 is pretty good tutorial which can help you.

      This tutorial will also explain you how to run multiple apps on different URLs, which will be helpful to you.

      If you have more questions, feel free to ask, I will answer if I'm able to help you. :)

      Node.js is an open source JavaScript runtime environment for easily building server-side and networking applications. Node.js applications can be run at the command line but this guide focuses on running them as a service using PM2, so that they will automatically restart on reboot or failure, and can safely be used in a production environment.
Have another answer? Share your knowledge.