9

the problem seems to be with mongoose & mongodb packages as it works fine when

mongoose.connect('mongodb+srv://mydb:<password>@cluster0.w1opr.mongodb.net/test?retryWrites=true&w=majority');

is removed it also works fine on repl.it cloud env here is my code

var express = require('express');
var ejs = require('ejs');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.connect('mongodb+srv://mydb:<password>@cluster0.w1opr.mongodb.net/test? 
retryWrites=true&w=majority');
app.set('view engine','ejs')
app.use(bodyParser.urlencoded({extended: true}));
.
.
.
app.listen(3000,function(){
console.log('Server is running on port 3000');
});
Seif Hafri
  • 93
  • 1
  • 1
  • 6

5 Answers5

35

Actually mongoose 6 requires Node 12 or higher, so this is expected behavior. Mongoose 6 does not support Node 10.So updating Node version will fix the issue. It also fix the problem by downgrading mongoose version to 5.

Muhammad Tariq
  • 396
  • 2
  • 5
6

Had the same problem when using tests. Setting the testEnvironment to node in my jest config fixed it (https://mongoosejs.com/docs/jest.html)

module.exports = {
  testEnvironment: 'node'
};
5

Check your node version, if it's lower than 12 it won't work, if that's the case updating node should do do the job. You could downgrade your mongoose version too.

There's an issue closed on Mongoose github page. https://github.com/Automattic/mongoose/issues/10638

JEmmerich
  • 51
  • 3
5

To get rid of this error in Windows upgrade to the latest version of Node.js by visiting this site https://nodejs.org/en/download/ and downloading the latest version of Node.js else if you do not want to upgrade to the latest version then you can get rid of this error by adding this line at the top of the file in node_modules/whatwg-url/dist/encoding.js:

const {TextDecoder, TextEncoder} = require("util");
0

For me i'm using UBUNUTU 20.04, as @MuhammadTariq said my node version was 10 and after upgrading the version to 16 the error is gone.

sh6210
  • 4,190
  • 1
  • 37
  • 27