next.js: [NEXT-1188] [13.4.x] ReferenceError: TextEncoder is not defined
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
❯ npx next info
Operating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 22.4.0: Mon Mar 6 21:00:17 PST 2023; root:xnu-8796.101.5~3/RELEASE_X86_64
Binaries:
Node: 18.14.0
npm: 9.3.1
Yarn: 1.22.19
pnpm: N/A
Relevant packages:
next: 13.4.1
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
Which area(s) of Next.js are affected? (leave empty if unsure)
No response
Link to the code that reproduces this issue
https://gitlab.com/biomedit/next-widgets/
To Reproduce
- Checkout the project
- Adapt next.js version
- Run
npm i && npm run test
Describe the Bug
Since v13.4.x, our test suites started to throw following error:
ReferenceError: TextEncoder is not defined
If we roll back to v13.3.4, the tests are green.
Somehow I have the impression that https://github.com/vercel/next.js/pull/49053 introduced the problem on our side but I am not sure 100% (I went through all the changes listed in the changelog).
Here is the MR where the problem started to appear. The MR did succeed because I reverted Next.js to v13.3.4.
Expected Behavior
Tests should be green.
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 14
- Comments: 21 (1 by maintainers)
It happened to me in production a bunch of times running on vercel
I encountered this exact same error on my team when running Jest tests. The error for us, appears to be coming through Apollo Client @apollo/client@3.7.14
Like @ribeaud the fix was to downgrade to v13.3.4. I bisected through it and for us, the error is introduced through this PR which removes node-fetch as a fallback and goes straight to undici.
I reverted the commit
e6acd40cba2c565919ee3e94bb9e8a100dc69495
(branch here) and everything works. Not the answer, but just some info. Even if I just return node-fetch and never undici things work fine.Otherwise, setting the global TextEncoder did get me a step further as suggested here, but ran into further issues
The below didn’t work for me but may work for someone else. For me the Apollo client would never return data
My info
I’m seeing the same issue in production in the same Chrome version as the reports above (
Chrome 84.0.4147
). Our Next.js version is14.0.4
.Same issue for me in production, got this in my sentry logs this morning (running 14.0.2).
If this can help!
having this issue as well
This happens sometimes to us in production Next.js 13.4.4
We’re on Next 14.0.1 and we had the same error in production this morning. We are not using jest though.
Same issue but on production without using jest.
For those of you experiencing this error when:
testEnvironment
isjsdom
Try adding this to the top of your test file:
See the jest docs for more.
https://github.com/vercel/next.js/blob/387430c659f12189f9a36b55570820954f9b3a5c/packages/next/src/client/app-index.tsx#L97
this class seems to be imported from the polyfill, which maybe is failing to build under some some circumstances
https://github.com/vercel/next.js/blob/387430c659f12189f9a36b55570820954f9b3a5c/packages/next/src/client/app-index.tsx#L2
Can confirm, this error is still coming through for chrome 84.0.4147 with nextjs 14.1.0 on production, no jest
@MenaiAla If most of your tests are for code executed in the node runtime, then going the opposite direction of setting the
testEnvironment
property of your jest config to'node'
(or omitting it entirely, since'node'
is the default) and then adding:to the top of tests for files where the code under test is typically executed in the browser might be the less painful route.
This, and my previous comment, is only applicable when the cause of this error is due to the mix up in runtimes.
Also spotted this issue in production. Same Chrome version as the repro above