Referenceerror: Textencoder Is Not Defined

Referenceerror: Textencoder Is Not Defined

I'm writing a simple addon in Firefox - 24, on Linux. I get the error:

ReferenceError: TextEncoder is not defined

when I do: var encoder = new TextEncoder(); the function I'm using is:

function write_text(filename, text) {
    var encoder = new TextEncoder();
    var data = encoder.encode(text);
    Task.spawn(function() {
       let pfh =  OS.File.open("/tmp/foo", {append: true});
       yield pfh.write(text);
       yield pfh.flush();
       yield pfh.close(); 
    });
}
1

17 Answers

if you are having this error while running node server

locate this file node_modules/whatwg-url/dist/encoding.js or .../lib/encoding.js

add this line at top const { TextEncoder, TextDecoder } = require("util");

2

In nodejs you can solve with util:

var util= require('util');
var encoder = new util.TextEncoder('utf-8');
0

If you experienced this because of using Mongodb via npm install mongodb then the simplest way is just to upgrade your Node Version. Needs to be higher than version 12; I used version 16 and it clearly fixed my problem

2
  1. This issue occurs in node 10 or lower version only. To resolve this issue upgrade node version to 12 or higher and then rm -rf node_modules && npm i

  2. Or If you don't want to upgrade node version, then,

    Locate this file

    node_modules/whatwg-url/dist/encoding.js // If dist folder
    
    node_modules/whatwg-url/lib/encoding.js // If lib folder
    

    And add this line in encoding.js file

    const { TextEncoder, TextDecoder } = require("./utils"); // if utils file
    
    const { TextEncoder, TextDecoder } = require("./util"); // if util file
    

Ah, you're using the SDK, I gather when re-reading the actual error of your other question.

  • You need to import TextEncoder explicitly from some other module, as SDK modules lack the class.
  • You need to yield OS.File.open.
  • append: is only supported in Firefox 27+
  • .flush() is only supported in Firefox 27+ (and a bad idea anyway). Use .writeAtomic if you need that.
  • You write: true to write to a file.

Here is a full, working example I tested in Firefox 25 (main.js)

const {Cu} = require("chrome");
// It is important to load TextEncoder like this using Cu.import()
// You cannot load it by just |Cu.import("resource://gre/modules/osfile.jsm");|
const {TextEncoder, OS} = Cu.import("resource://gre/modules/osfile.jsm", {});
const {Task} = Cu.import("resource://gre/modules/Task.jsm", {});

function write_text(filename, text) {
    var encoder = new TextEncoder();
    var data = encoder.encode(text);
    filename = OS.Path.join(OS.Constants.Path.tmpDir, filename);
    Task.spawn(function() {
       let file = yield OS.File.open(filename, {write: true});
       yield file.write(data);
       yield file.close(); 
       console.log("written to", filename);
    }).then(null, function(e) console.error(e));
}

write_text("foo", "some text");

I was also getting this error so I solved it in this way, in nodejs project go to the node_modules/whatwg-url/dist/encoding.js file in that add this line =>

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

And your problem is solved

1

A text encoder for Node.js can be found in the util module. You can access it like so:

const util = require('util');
const TextEncoder = new util.TextEncoder();

One of the roles of the TextEncoder is to convert a string of text into an array of bytes. You can achieve this like so:

const data = TextEncoder.encode(
   JSON.stringify({ c: "Green" })
);
// Uint8Array [ 123, 34, 99, 34, 58, 34, 71, 114, 101, 101, 110, 34, 125 ]

The array returned is called a Uint8Array. It consists of integers in the range 0 to 255.

Note that TextEncoder only supports UTF-8 encoding.

If it's an error in node_modules/whatwg_url/dist/encoding.js folder then uninstall MongoDB by

npm uninstall mongodb

and reinstall it

npm install --save mongodb
2

For me upgrade Node.js version resolved this issue

The TextEncoder can be found in the sdk/io/buffer module:

let { TextEncoder, TextDecoder } = require('sdk/io/buffer')

I was also facing the same problem in my project but I fixed this issue by upgrading my node version from 10 to 12. May be this issue now a days can occurred due to lower version of node we are using in our project.

This looks like a node version error because I solved it by updating from 10 to 16 and after that, I installed dependencies and open a new terminal.

  1. Update node to 14 or higher, I used Node Version Manager (NVM)

  2. Delete node_modules directory, on linux:

    rm -rf node_modules

  3. Install dependencies with npm install

  4. Close and open a new terminal

  5. Run app with node or nodemon

Done!

delete your 'node_modules' folder

rm -rf node_modules

then re-install dependencies

npm i

If you are using 'mongoose' greater than v6 you need at least Node.js v12

I was facing the same error, because of having to install old nodejs. This problem can be solved by installing the latest nodejs. To update nodejs to nodejs to 14.x

sudo apt update curl -sL | sudo bash - sudo apt install -y nodejs node -v

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

And in place of:

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

Write this code:

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

all you where missing is this small part by including utils.

This might help others.

I was getting the same error and I almost tried all the above solutions, but nothing works for me. Finally, I update the npm version and everything is fine.

When I installed the Next App the npm version was 6.14.4.

I update the version and all errors are fixed you don't need to change anything in the core files just update the version in my case recommended 8.11.0.

npm -v // Check the version

npm install -g npm@latest // Get the latest version

OR

npm install -g npm@8.11.0 // Get the Spacific version

Complete guid here

TextEncoder is native function in javascript, check the version that suit the ability.

Chrome version >=38, Edge version >=79, Firefox version >=18, Node verison >=11.0.0...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor
Author

Sophia Al-Mansoor

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.