Craig Glennie

Fixing Squoosh error when building NextJS Docker image

You are using Next’s <Image /> component but when you try to do RUN npm next build in your Dockerfile you get an error from squoosh (a library for image optimisation that Next uses).

#14 18.60 ./public/myImage.jpg
#14 18.60 TypeError: Failed to parse URL from /app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_enc.wasm
#14 18.60     at new Request (node:internal/deps/undici/undici:4832:19)
#14 18.60     at Agent.fetch2 (node:internal/deps/undici/undici:5524:29)
#14 18.60     at Object.fetch (node:internal/deps/undici/undici:6351:20)
#14 18.60     at fetch (node:internal/bootstrap/pre_execution:197:25)
#14 18.60     at instantiateAsync (/app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_enc.js:424:28)
#14 18.60     at createWasm (/app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_enc.js:447:13)
#14 18.60     at /app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_enc.js:1570:19
#14 18.60     at Object.instantiateEmscriptenWasm (/app/node_modules/next/dist/server/lib/squoosh/emscripten-utils.js:15:12)
#14 18.60     at Object.enc (/app/node_modules/next/dist/server/lib/squoosh/codecs.js:158:42)
#14 18.60     at Object.encodeJpeg (/app/node_modules/next/dist/server/lib/squoosh/impl.js:80:23)

The problem may be that you are trying to use Node 18 and squoosh does not currently support it, or at least the version of squoosh that NextJS currently uses does not. AFAIK to get around this you need to change your Node version to 16. Squoosh will support Node 18 soon but not yet.