最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReferenceError: TextEncoder is not defined when importing jsdom library - Stack Overflow

programmeradmin0浏览0评论

I have these two files:

test.js:

const jsdom = require("jsdom");

const hello  = () =>  {
  console.log("hello!");
};


module.exports = {
    hello
};

and

server.js:

const hello = require('./stuff/test');

hello.hello();

directory structure:

myprojectfolder
             backend
                    src
                       stuff
                           test.js
                       server.js

When I run server.js I get the ReferenceError: TextEncoder is not defined error:

/home/myusername/projects/myprojectfolder/node_modules/whatwg-url/lib/encoding.js:2
const utf8Encoder = new TextEncoder();
                    ^

ReferenceError: TextEncoder is not defined

If I remove const jsdom = require("jsdom"); line from test.js, server.js runs fine and without any errors (outputs hello).

Why does it happen and how do I fix it (while still being able to import and use jsdom inside test.js?).

I have these two files:

test.js:

const jsdom = require("jsdom");

const hello  = () =>  {
  console.log("hello!");
};


module.exports = {
    hello
};

and

server.js:

const hello = require('./stuff/test');

hello.hello();

directory structure:

myprojectfolder
             backend
                    src
                       stuff
                           test.js
                       server.js

When I run server.js I get the ReferenceError: TextEncoder is not defined error:

/home/myusername/projects/myprojectfolder/node_modules/whatwg-url/lib/encoding.js:2
const utf8Encoder = new TextEncoder();
                    ^

ReferenceError: TextEncoder is not defined

If I remove const jsdom = require("jsdom"); line from test.js, server.js runs fine and without any errors (outputs hello).

Why does it happen and how do I fix it (while still being able to import and use jsdom inside test.js?).

Share Improve this question edited Feb 17, 2022 at 19:03 Boaz 20.2k9 gold badges66 silver badges72 bronze badges asked Feb 17, 2022 at 18:55 parsecerparsecer 5,17619 gold badges84 silver badges167 bronze badges 5
  • 1 Is this in Node.js and if so which version? – Boaz Commented Feb 17, 2022 at 18:57
  • Does this answer your question? ReferenceError: TextEncoder is not defined – Boaz Commented Feb 17, 2022 at 19:01
  • @Boaz I was using NodeJS version 10, tried version 16 and it runs fine, thank you! – parsecer Commented Feb 17, 2022 at 19:02
  • 2 Indeed, the TextEncoder constructor is only available (globally) from Node 12. This is covered in the duplicate post. – Boaz Commented Feb 17, 2022 at 19:03
  • 4 I'm using nodejs v18 and still get the error. I even installed text-encoder. Still doesn't work. – John Tang Boyland Commented Sep 30, 2022 at 0:43
Add a ment  | 

1 Answer 1

Reset to default 7

Well, it took me all day, but finally I managed to pull it together. My problem was that tests didn't run because of that error.

So, in package.json under "scripts" I added the following:

"test": "jest --verbose --runInBand --detectOpenHandles --forceExit"

And in jest.config.js, I added the following:

   globals: {
      "ts-jest": {
         tsConfigFile: "tsconfig.json"
       },
       TextEncoder: require("util").TextEncoder,
       TextDecoder: require("util").TextDecoder
   }
发布评论

评论列表(0)

  1. 暂无评论