Namecheap Node.js app cannot connect to Atlas

Please help! My NodeJS application I hosted on Namecheap cannot connect to MongoDB Atlas.
I get the following error:

Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
    at NativeConnection.Connection.openUri (/home/paciojgq/nodevenv/loan/12/lib/node_modules/mongoose/lib/connection.js:830:32)
    at Mongoose.connect (/home/paciojgq/nodevenv/loan/12/lib/node_modules/mongoose/lib/index.js:335:15)
    at Object.<anonymous> (/home/paciojgq/loan/models/db.js:30:10)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Module.require (internal/modules/cjs/loader.js:830:19)
    at require (internal/modules/cjs/helpers.js:68:18)
    at Object.<anonymous> (/home/paciojgq/loan/app.js:2:1)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
    at internal/main/run_main_module.js:17:11
(node:74432) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:74432) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
1 Like

As the error message indicate

but in this case the recommendation

is misleading. What needs to be white listed is the IP of your app running on Namecheap.

2 Likes

Please how do I know the ip of my app running on Namecheap?
Thanks!

I do not know that service so I do not know what tools you can use. The best is to contact technical support of this provider.

:heart:
Alright
Thanks

I agree with everyone’s suggestions. The problem is that the IP address isn’t Whitelisted on Atlas (this is a security feature to make sure bad actors do not try to access your MongoDB Database). Getting the IP address depends on the kind of hosting you have from NameCheap. You might have a shared or dedicated IP address depending on your plan. It appears that you can contact NameCheap support to request your IP Address.

Let me know if you have any questions or if you were able to resolve your issue.

2 Likes

I seem to have a similar issue. I whitelisted my IP but still, it wont work. I was also in contact with the namecheap support but they could not find the problem. It works fine when I whitelist all IPs on MongoDB but adding the correct IP from my shared Namecheap server won’t work. And I also checked the DNS A Record. Its pointed to the correct IP.
Since it still work when all IPs are whitlsited, is there a way to analyze (check) the IP from which a database was accessed. This way I could access it and then check what IP data was received by mongoDB . Is this possible?

Try to go to https://www.whatismyip.com/ from the machine where your app is running. It should give the public IP address seen by the cluster.

thanks a lot! But its the same IP which I already whitelisted. Still, it’s not working. Therefore, I have to give access to every IP. It’s a little strange. Some rule of MongoDB seems to reject it even do its the correct IP.

Share the exact error message you get when you only allow access from the whatismyip result.

I can not do it right now but what basically happens it that it seems to try to connect around 20 seconds and then it though an error like: “make sure you have whitelisted the IP from where you are accessing”

That is why it would be more helpful to analyze it backwards.

  1. Give access to all IPs
  2. Fetch data from the problematic IP
  3. Check SOMEWHERE on MongoDB which IP was used to fetch the data
  4. Check the differences from this IP to the whitelisted IP which is stated as IP on Namecheap

Of course, normally it should be the same as from “whatismyip”. But it would be interesting to check exactly this.

Late update but encountered the same issue today.

As mentioned above, the “Shared IP Address” shown in the Namecheap cPanel doesn’t provide access when whitelisted.

What worked for me was to enable a wider IP range (no need to enable all IPs!) using https://www.ipaddressguide.com/cidr to generate the “CIDR-notated range of addresses”.

Example - if your Namecheap Shared IP is 199.198.197.19, your range might be from 199.198.197.0 to 199.198.197.255, this can be whitelisted successfully as: 199.198.197.0/24

Hope this helps!