UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError
If you are using mongoDB.Atlas go to network access and set Whitelist Entry: 0.0.0.0/0.
Re-run your command.
While the above solution might work, it is better to understand on how to debug and what the error says.
You got UnhandledPromiseRejectionWarning
, because in your code, you must not be handling the promise rejection case where you are connection to MongoDB.
Next, the node server failed to connect to the mongodb. This could be because of several reasons. One simple case as mentioned by @Sayegh, the server itself is not running. Could be other reason like wrong port etc, server not accepting incoming connections etc.
How to debug. First step is to figure out if the process is running or not. One simple way would be to run this command ps aux | grep mongo
which list all processes from all users with mongo as a text.
If this was true and still unable to connect, look for port. If still unable to connect, check if the server accepts incoming connection and take it from there.
Not sure what system you're on, but try running this if you're on Linux:
mongo
or for macOS:
mongod
and leave it running in a new terminal and then try running the server again