Check the connection string that is used for authenticating with MongoDB server.

When trying to connect with MongoDB Compass connection may be working and it might fail when connecting via NodeJS code. Check if authSource parameter and the ssl param are properly set.

Sample MongoDB URI that works on NodeJS connection setup is shown below:

MONGO_URI=mongodb://USER:Password@localhost:27017/SampleDatabase?authSource=admin&readPreference=primary&directConnection=true&ssl=false