whatwg-url: ReferenceError: TextEncoder is not defined
Relative from: https://github.com/Automattic/mongoose/issues/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.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 20 (2 by maintainers)
Commits related to this issue
- chore: use jsdom-custom.js to re-add TextEncoder and TextDecoder Reference: - https://github.com/jsdom/whatwg-url/issues/209 — committed to influxdata/ui by jdstrand 2 years ago
- chore: use jsdom-custom.js to re-add TextEncoder and TextDecoder Reference: - https://github.com/jsdom/whatwg-url/issues/209 — committed to influxdata/ui by jdstrand 2 years ago
- chore: use jsdom-custom.js to re-add TextEncoder and TextDecoder Reference: - https://github.com/jsdom/whatwg-url/issues/209 — committed to influxdata/ui by jdstrand 2 years ago
- chore: use jsdom-custom.js to re-add TextEncoder and TextDecoder Reference: - https://github.com/jsdom/whatwg-url/issues/209 — committed to influxdata/ui by jdstrand 2 years ago
- chore: use resolutions for cross-fetch (#4713) * chore: use resolution to resolve cross-fetch to 3.1.5 * chore: use jsdom-custom.js to re-add TextEncoder and TextDecoder Reference: - https://g... — committed to influxdata/ui by jdstrand 2 years ago
- Fix for downstream whatwg-url issue jsdom/whatwg-url#209 — committed to SimenB/react-router by timdorr a year ago
- Fix for downstream whatwg-url issue jsdom/whatwg-url#209 — committed to remix-run/react-router by timdorr a year ago
- Fix for downstream whatwg-url issue jsdom/whatwg-url#209 — committed to remix-run/react-router by timdorr a year ago
You need to provide an example that only uses whatwg-url, no other packages, that we can run with
node test.js
.@dzek69 I’ve added
console.log(process.version)
before thatnew TextEncoder();
line and it logsv16.13.2
.Had to add the following to jest setup file:
The same issue on the nodejs v16.13.0
This happened to me when I used jest for testing and I got
jest.config.js
that hadtestEnvironment
set tojsdom
, and unnecessarily requiredjsdom
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 likeTextEncoder
are also gotten rid of, and this affects the modules that depend on those functions and are required by the test case.Is a node version problem: https://github.com/Automattic/mongoose/issues/10713#issuecomment-921373677
Installing Node 18 fixed it for me.
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash - apt install nodejs
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
andnodejs
version numbers.Update: yeah, Installing Node 12.22.7 fixed it for me.
I am having the same issue
@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.
@dzek69
logs
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.
this stackoverflow helped me https://stackoverflow.com/a/69187990/3642044
@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 terminali 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
.Indeed, your version is too old. Node v10 is not supported.