admin管理员组文章数量:1305569
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
?).
- 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 version16
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
1 Answer
Reset to default 7Well, 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
}
本文标签: javascriptReferenceError TextEncoder is not defined when importing jsdom libraryStack Overflow
版权声明:本文标题:javascript - ReferenceError: TextEncoder is not defined when importing jsdom library - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741805879a2398501.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论