Reputation:
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();
});
}
Upvotes: 61
Views: 190641
Reputation: 94
I'm facing the same issue and have found a solution for it. Please refer below link for a solution, it works for me.
https://stackoverflow.com/a/78861162/22258697
Upvotes: 1
Reputation: 1
in case you get this error while running tests: https://stackoverflow.com/a/74864115
Setting the testEnvironment to node in my jest.config
file fixed it
(https://mongoosejs.com/docs/jest.html)
module.exports = {
testEnvironment: 'node'
};
Upvotes: -1
Reputation: 1409
I was getitng the same error after adding the mongodb, i resolve it by upgrading to node version
nvm use 16.16.0
Upvotes: 0
Reputation: 6604
I encountered this when running automated tests with jest and rendering a component that included import { AgGridColumn, AgGridReact } from "ag-grid-react"
.
The solution is to mock out that function as follows:
jest.mock('ag-grid-react', () => ({
__esModule: true,
AgGridReact: jest.fn((x) => <div>{x.children}</div>),
AgGridColumn: jest.fn(() => <div />),
}));
Upvotes: 1
Reputation: 780
In my case, I had multiple node versions installed, and current project required a more recent node version. To fix the problem I did the following steps.
To check the current version of node running, in the terminal use the command
node --version
Output in the Terminal :
The following command will list the different node versions already installed in your system.
nvm ls
Output in the Terminal :
To switch to more recent version of the node ie, v16.17.0, use the following command in the terminal
nvm use v16.17.0
Output in the Terminal :
Now confirm the current version of the node by
node --version
Output in Terminal:
Upvotes: 0
Reputation: 31
It's a node version problem.
Described by @yhojann-cl here
I have the same problem:
In /usr/bin/node
I have 10.x, but 16.x is installed by nvm
.
Upvotes: 0
Reputation: 657
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");
Upvotes: 60
Reputation: 763
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
Upvotes: 0
Reputation: 76
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.
Upvotes: 2
Reputation: 2500
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
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
Upvotes: 15
Reputation: 61
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 https://deb.nodesource.com/setup_14.x | sudo bash - sudo apt install -y nodejs node -v
Upvotes: 0
Reputation: 291
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.
Update node to 14 or higher, I used Node Version Manager (NVM)
Delete node_modules directory, on linux:
rm -rf node_modules
Install dependencies with npm install
Close and open a new terminal
Run app with node or nodemon
Done!
Upvotes: 1
Reputation: 420
TextEncoder is native function in javascript, check the version that suit the ability.
https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder#browser_compatibility
Chrome version >=38, Edge version >=79, Firefox version >=18, Node verison >=11.0.0...
Upvotes: -2
Reputation: 499
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.
Upvotes: 1
Reputation: 7384
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
Upvotes: 21
Reputation: 61
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
Upvotes: 5
Reputation: 100
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
Upvotes: 1
Reputation: 3334
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.
Upvotes: 3
Reputation: 515
In nodejs you can solve with util:
var util= require('util');
var encoder = new util.TextEncoder('utf-8');
Upvotes: 30
Reputation: 1268
The TextEncoder
can be found in the sdk/io/buffer
module:
let { TextEncoder, TextDecoder } = require('sdk/io/buffer')
Upvotes: 1
Reputation: 33192
Ah, you're using the SDK, I gather when re-reading the actual error of your other question.
TextEncoder
explicitly from some other module, as SDK modules lack the class.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.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");
Upvotes: 6