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
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 });
Open your encoding.js folder in node_modules
Replace these line of code
"use strict";
const utf8Encoder = new TextEncoder();
const utf8Decoder = new TextDecoder("utf-8", { ignoreBOM: true });
With this --
"use strict";
var util= require('util');
const utf8Encoder = new util.TextEncoder();
const utf8Decoder = new util.TextDecoder("utf-8", { ignoreBOM: true });
Then you are good to go.
This seems to be an issue with older node versions. You need to use the latest node. If you are using nvm type
nvm use node
Note: to download latest node with nvm use this command
nvm install --lts