Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceError: TextEncoder is not defined #209

Closed
yhojann-cl opened this issue Sep 12, 2021 · 20 comments
Closed

ReferenceError: TextEncoder is not defined #209

yhojann-cl opened this issue Sep 12, 2021 · 20 comments

Comments

@yhojann-cl
Copy link

yhojann-cl commented Sep 12, 2021

Relative from: Automattic/mongoose#10713

In https://github.com/jsdom/whatwg-url/blob/master/src/encoding.js line 2 says:

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

But node show an error message:

/app/node_modules/whatwg-url/dist/encoding.js:2
const utf8Encoder = new TextEncoder();
                    ^

ReferenceError: TextEncoder is not defined
    at Object.<anonymous> (/app/node_modules/whatwg-url/dist/encoding.js:2:21)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/app/node_modules/whatwg-url/dist/url-state-machine.js:5:34)
    at Module._compile (internal/modules/cjs/loader.js:778:30)

My node version:

$ node -v
v14.17.5

$ npm -v
6.14.14

The version in package.json of dependency is:

{
  "_from": "whatwg-url@^9.1.0",
  "_id": "whatwg-url@9.1.0",

I installed from mongoose package but have last version of whatwg-url package.

@domenic
Copy link
Member

domenic commented Sep 12, 2021

You need to provide an example that only uses whatwg-url, no other packages, that we can run with node test.js.

@domenic domenic closed this as completed Sep 12, 2021
@camperjett
Copy link

I am having the same issue

@yhojann-cl
Copy link
Author

yhojann-cl commented Sep 24, 2021

Is a node version problem: Automattic/mongoose#10713 (comment)

@sarmiSiva
Copy link

no, it's not working.

@wyatt8740
Copy link

wyatt8740 commented Dec 12, 2021

It is not a node version problem - unless by that you mean my version of nodeJS is too old (which is quite possible)? The comment you linked is a problem of mismatches, from how they phrase it.

There is no mismatch between node and nodejs version numbers.

$ node -v
v10.20.1
$ nodejs -v
v10.20.1

$ node ./server.js
/path/to/project/node_modules/whatwg-url/lib/encoding.js:2
const utf8Encoder = new TextEncoder();
                    ^

ReferenceError: TextEncoder is not defined
    at Object.<anonymous> (/path/to/project/node_modules/whatwg-url/lib/encoding.js:2:21)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/path/to/project/node_modules/whatwg-url/lib/url-state-machine.js:5:34)
    at Module._compile (internal/modules/cjs/loader.js:778:30)

Update: yeah, Installing Node 12.22.7 fixed it for me.

@domenic
Copy link
Member

domenic commented Dec 12, 2021

Indeed, your version is too old. Node v10 is not supported.

@Unrasend
Copy link

Unrasend commented Dec 13, 2021

The same issue on the nodejs v16.13.0

@dzek69
Copy link

dzek69 commented Dec 13, 2021

@Unrasend it won't happen on v16, you probably have multiple versions of node in your system and something you use to run your code may use different version from what you have when you type node in your terminal

i just solved the same issue for someone. IDE was using one version of node (10.x) when running tests, while on the project there was some nvm config that was using 16.x when running tests via npm run.

@bustedware
Copy link

this stackoverflow helped me https://stackoverflow.com/a/69187990/3642044

@FINDarkside
Copy link

FINDarkside commented Jan 18, 2022

@dzek69 I've added console.log(process.version) before that new TextEncoder(); line and it logs v16.13.2.

Had to add the following to jest setup file:

import { TextEncoder, TextDecoder } from 'util';
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;

@alexKarvounis
Copy link

@FINDarkside 's solution solved it for me.

@dzek69
Copy link

dzek69 commented Jan 21, 2022

@FINDarkside what's your error message then? Maybe you run something before that mess up the global object?

The simplest:

console.log(process.version);
console.log(new TextEncoder());

run on node 16.13.2 gives:

v16.13.2
{ encoding: 'utf-8' }

Which is expected, docs for that specific version says it's in the global:
https://nodejs.org/dist./v16.13.2/docs/api/util.html#class-utiltextencoder

@FINDarkside
Copy link

FINDarkside commented Jan 21, 2022

@dzek69

console.log(process.version);
console.log(typeof TextEncoder);

logs

v16.13.2
undefined

This seems to be some kind of issue with jest, jsdom, next.js default test setup or something else, but I won't look into it more since the snippet above solved the issue for me.

@mooncorn
Copy link

mooncorn commented Feb 2, 2022

Seems like this issue appears on older versions of node. Updating node to 16.x solved the problem for me.

@moriyoshi
Copy link

moriyoshi commented May 16, 2022

This happened to me when I used jest for testing and I got jest.config.js that had testEnvironment set to jsdom, and unnecessarily required jsdom in the test case. Since the global scope should have been already refurbished so it mimicks the browser environment before jest tries to run the test case, node specific global functions/classes like TextEncoder are also gotten rid of, and this affects the modules that depend on those functions and are required by the test case.

jdstrand added a commit to influxdata/ui that referenced this issue May 31, 2022
jdstrand added a commit to influxdata/ui that referenced this issue May 31, 2022
jdstrand added a commit to influxdata/ui that referenced this issue May 31, 2022
jdstrand added a commit to influxdata/ui that referenced this issue Jun 1, 2022
jdstrand added a commit to influxdata/ui that referenced this issue Jun 7, 2022
* chore: use resolution to resolve cross-fetch to 3.1.5

* chore: use jsdom-custom.js to re-add TextEncoder and TextDecoder

Reference:
- jsdom/whatwg-url#209
@BigOldWei
Copy link

Installing Node 18 fixed it for me.

curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
apt install nodejs

timdorr added a commit to SimenB/react-router that referenced this issue Jan 12, 2023
timdorr added a commit to remix-run/react-router that referenced this issue Jan 12, 2023
timdorr added a commit to remix-run/react-router that referenced this issue Jan 18, 2023
@Allislove
Copy link

Allislove commented Mar 4, 2023

No es un problema de versión del nodo, a menos que quiera decir que mi versión de nodeJS es demasiado antigua (lo cual es muy posible). El comentario que vinculó es un problema de desajustes, por cómo lo expresan.

No hay discrepancia entre nodey nodejsnúmeros de versión.

$ node -v
v10.20.1
$ nodejs -v
v10.20.1

$ node ./server.js
/path/to/project/node_modules/whatwg-url/lib/encoding.js:2
const utf8Encoder = new TextEncoder();
                    ^

ReferenceError: TextEncoder is not defined
    at Object.<anonymous> (/path/to/project/node_modules/whatwg-url/lib/encoding.js:2:21)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/path/to/project/node_modules/whatwg-url/lib/url-state-machine.js:5:34)
    at Module._compile (internal/modules/cjs/loader.js:778:30)

Actualización: sí, la instalación de Node 12.22.7 me lo arregló.

This is the way, thanks a lot. @wyatt8740:
Just do in the terminal.

sudo apt update -y && sudo apt upgrade -y

curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash -

sudo apt install nodejs

node -v

@valdineifer
Copy link

valdineifer commented May 19, 2023

i just solved the same issue for someone. IDE was using one version of node (10.x) when running tests, while on the project there was some nvm config that was using 16.x when running tests via npm run.

@dzek69 really helped me now. I have nvm installed and I didn't know that it existed an node v10 installed without nvm.
I'm just emphasizing the reply to (maybe) help other users.

@Dantemss
Copy link

To make it work in node 10 (which is what the install script installs):

Add this to resolutions in xapi/package.json:

"whatwg-url": "<9.0.0"

Run yarn --ignore-engines to update yarn.lock

@christopher-theagen
Copy link

.jest/setup.js

import { TextEncoder, TextDecoder } from 'util';

global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests