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

Most upvoted comments

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 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;

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 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.

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 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.

I am having the same issue

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.

@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.

@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.

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