10

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

7 Answers7

20

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 });
Community
  • 1
  • 1
Aakash
  • 139
  • 1
  • 3
  • 22
5

This worked for me at the top of my small script file.

"use strict";
const util = require('util');
global.TextEncoder = util.TextEncoder;
global.TextDecoder = util.TextDecoder;
Rob Evans
  • 2,822
  • 1
  • 9
  • 15
3

It's a compatibility issue with node.js version, upgrade your node.js version and then reinstall node packages.

Upgrade your version with: v16.14.2

It's not recommended to modify files inside the node_modules folder

You can use NVM to manage multiple node.js versions, here are the installation guide: https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04

Kishor Patidar
  • 623
  • 12
  • 23
  • Seems not necessary to go to `v16.14.2`. Just fixed this by going to `v14.21.1`. Version `v14.17.4` works as well, and probably several before that. – tanius Jan 03 '23 at 01:47
1

Open your encoding.js folder in node_modules

  1. Open the node_modules
  2. Locate the whawg-url folder and open.
  3. Search for dist folder - there, you'd find the encoding.js folder
  4. Open via VS Code or any IDE of your choice.

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.

1

TextEncoder is not defined node error

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
Nimantha
  • 6,405
  • 6
  • 28
  • 69
ZowWeb
  • 69
  • 7
0

This is because your using an old version of node, if you install node using apt install node, you get an old version. So you need to upgrade to a recent version of node.

Armar
  • 249
  • 3
  • 7
0

You just need a compatible node version of libraries in package.json. I encountered the same error the mistake I made is that I used nvm and I ran npm install with node v12.x.x for some reason nvm did not set the current version it set v10.x.x when I returned to the project folder. so here what I resolved the error:

$ rm -rf node_modules
# use another way to switch to the correct version if not using nvm
$ nvm use v12.x.x # Switch node version to v12.x.x
$ npm install
# then restart the server

if you use nvm and you want to set the project node version automatically when entering the project folder, just follow this instruction.

Kiry Meas
  • 1,200
  • 13
  • 26