next.js: Build Fails with Terser: Invalid Unicode Code Point Error

Link to the code that reproduces this issue

https://github.com/PBnJson/reproduction-template

To Reproduce

  1. Set Up a Next.js Project: Use Next.js version 14.2.5 with next-pwa for PWA functionality.
  2. Configure next.config.js: Enable swcMinify and set experimental.newNextLinkBehavior to true.
  3. Run the Build Command: Execute npm run build.
  4. Observe the Error: Notice the build fails with a Terser error related to an invalid Unicode code point.

Current vs. Expected behavior

I expected the project to build and be minified but instead I had to disable minification with swcMinify:false in order to build successfully.

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Home
  Available memory (MB): 7954
  Available CPU cores: 8
Binaries:
  Node: 20.14.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.2.5
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Runtime, Script (next/script)

Which stage(s) are affected? (Select all that apply)

Other (Deployed)

Additional context

I’m deploying in Google Cloud vm instance with Debian. The app was working great before I added the service workers. The error happens at invalid unicode code point at line 1 column 43757 when I run docker compose up --build. Disabling swcMinify in next.config.js got the build to succeed, so some sort of minification issue.

  • I was unable to reproduce the error in the reproduction-template Screenshot 2024-08-24 144218

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 26
  • Comments: 25 (1 by maintainers)

Commits related to this issue

Most upvoted comments

This is caused by Node.js 22.7.0, downgrading to Node.js 22.6.0 fixes this error. See #69150

also happens to me but only when building docker image, which is weird. i’ve tried alpine, debian and even archlinux as base images, same error

my nextjs.config.js has nothing but rewrites in it

using next 14.2.5

Had this problem with node:alpine Docker image. Rolling back to node:lts-alpine fixed it.

node:lts-alpine is a good choice if you’re sticking with alpine to avoid using the latest version of Node.

I choice to revisit the use of alpine and found the recommendation given by Snyk in this article to be quite helpful.

https://snyk.io/blog/choosing-the-best-node-js-docker-image/

Maybe it will help some of you to choose the most appropriate Node Docker image too.

The authors of the article suggested using node:lts-bookworm-slim for security and performance reasons

FWIW, I had no issues with my builds using Node version 20.17.0 up until 2 days ago, on August 23rd. Prior to that, deployments would succeed without this invalid unicode code point error.

So, it seems to be a very new issue with how strings are being parsed by Terser.

I tried downgrading to Node version 20.10.0 as suggested by @C3ntraX, but still no luck

I had the same issue. Seems like that the latest node version does not work with terser and will throw this wreid error.

I used node-version: “20.10.0” which works for me.

but Next.js is removing the ability to disable the SWC Minifier in the next major release, Next.js 15.

Yeah, I understand, but Sunday night is not the time for me to deal with this 😅

Catching the same problem - Nextjs 13.2.4 / Docker / Github actions / Nodejs slim 16

#8 20.95 info  - Creating an optimized production build...
#8 68.91 Failed to compile.
#8 68.91 
#8 68.91 static/chunks/2617-257e92d53d9ab516.js from Terser
#8 68.91 invalid unicode code point at line 1 column 114079
#8 68.91 Error: invalid unicode code point at line 1 column 114079
#8 68.91     at Object.minify (/app/node_modules/next/dist/build/swc/index.js:363:33)
#8 68.91     at Object.minify (/app/node_modules/next/dist/build/swc/index.js:465:21)
#8 68.91     at async Object.minify (/app/node_modules/next/dist/build/webpack/plugins/terser-webpack-plugin/src/index.js:146:44)
#8 68.91     at async /app/node_modules/next/dist/build/webpack/plugins/terser-webpack-plugin/src/index.js:202:42
#8 68.91     at async Span.traceAsyncFn (/app/node_modules/next/dist/trace/trace.js:79:20)

Tried versions 20.14.0 and 20.10.0 - did not solve the problem.

As a temporary solution I disabled swc in next.config.js.

Solution for me was switching from ‘node:bullseye’ -> ‘node:22.6-bullseye’ in the docker image.

node:bullseye started using 22.7.0 when it was available I guess and that made our pipeline explode

Had this problem with node:alpine Docker image. Rolling back to node:lts-alpine fixed it.

It builds successfully in local, but fails in ci/cd process like github actions.

Catching the same problem - Nextjs 13.2.4 / Docker / Github actions / Nodejs slim 16

#8 20.95 info  - Creating an optimized production build...
#8 68.91 Failed to compile.
#8 68.91 
#8 68.91 static/chunks/2617-257e92d53d9ab516.js from Terser
#8 68.91 invalid unicode code point at line 1 column 114079
#8 68.91 Error: invalid unicode code point at line 1 column 114079
#8 68.91     at Object.minify (/app/node_modules/next/dist/build/swc/index.js:363:33)
#8 68.91     at Object.minify (/app/node_modules/next/dist/build/swc/index.js:465:21)
#8 68.91     at async Object.minify (/app/node_modules/next/dist/build/webpack/plugins/terser-webpack-plugin/src/index.js:146:44)
#8 68.91     at async /app/node_modules/next/dist/build/webpack/plugins/terser-webpack-plugin/src/index.js:202:42
#8 68.91     at async Span.traceAsyncFn (/app/node_modules/next/dist/trace/trace.js:79:20)

Tried versions 20.14.0 and 20.10.0 - did not solve the problem.

As a temporary solution I disabled swc in next.config.js.

I solved this issue temporarily thanks to comment I replied. However, the swcMinify option in next.config.mjs will be enabled by default and removed in Next.js 15. This issue must be resolved before Next.js 15 is released.

image

also happens to me but only when building docker image, which is weird. i’ve tried alpine, debian and even archlinux as base images, same error

my nextjs.config.js has nothing but rewrites in it

using next 14.2.5

I can confirm that the issue only happens when building my Next.js v14.2.6 app as a Docker image. I do not experience the error at all when I build my Next.js app locally; the build always fails in my GitHub Actions CI/CD script.

I was able to isolate the file which causes the issue; however, I haven’t made any significant changes since before this issue arose. That change is 3 lines which makes no use of string manipulation, only array filtering and the use of .includes on a string.

I would share the file, but I think it is unnecessary, given my previous sentence.

I think I’ve found a consistent way to trigger the error.

One of these lines triggers the error, other does not: https://github.com/eyenalxai/paste/blob/97e9821d709ff25e1ea7686a72aed9970a84a8af/lib/syntax/detect.ts#L34

To reproduce:

  1. git clone https://github.com/eyenalxai/paste.git -b terser-error && cd paste
  2. docker build -t paste:latest .

This will result in an error.

Then, swap the comments on the lines as follows:

// const what = ooofs // Terser error
const what = "what" // No error

Run docker build -t paste:latest . again, should build successfully.

The same thing happened to me on when building Next.js on docker in Github Actions for Cypress E2E tests.

https://github.com/cypress-io/github-action?tab=readme-ov-file#docker-image

▲ Next.js 14.2.5

Creating an optimized production build … Failed to compile.

static/chunks/516-dd8f1e03298072f7.js from Terser invalid unicode code point at line 1 column 76631 Error: invalid unicode code point at line 1 column 76631