const utf8Encoder = new TextEncoder(); in Node js

I am trying to use mongodb so I install mongoose package

but the problem is when I am writing like this

const express = require("express");
const dotenv = require("dotenv");
const mongoose = require("mongoose"); //getting error here

It showing me error like this

const utf8Encoder = new TextEncoder();
                    ^

ReferenceError: TextEncoder is not defined

If I am commenting mongoose line I don’t get any error but I need to use this even i checked my node version its 16.5.0 I tried looking an old post where same error occur but its not understandable to me any help ? old post link enter link description here

how to fix this error

Answer

Open your encoding.js folder in node_modules>whatwg-url>dist

and write this code

"use strict";
var util= require('util');
const utf8Encoder = new util.TextEncoder();
const utf8Decoder = new util.TextDecoder("utf-8", { ignoreBOM: true });

in place of

"use strict";
const utf8Encoder = new TextEncoder();
const utf8Decoder = new TextDecoder("utf-8", { ignoreBOM: true });

all you where missing is this small part by including utils

var util= require('util');
const utf8Encoder = new util.TextEncoder();
const utf8Decoder = new util.TextEncoder("utf-8", { ignoreBOM: true });