Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: TextEncoder is not defined Node.js with mongoose

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');
});
like image 306
Seif Hafri Avatar asked Sep 01 '21 14:09

Seif Hafri


2 Answers

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.

like image 55
Muhammad Tariq Avatar answered Sep 21 '22 02:09

Muhammad Tariq


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

like image 41
JEmmerich Avatar answered Sep 20 '22 02:09

JEmmerich