We have a next.js 12 project built and run on node 16 which works fine but we want to migrate to newer nodes. Going for the next LTS node 18 is a logical step, and next dev
works fine, but running next build
results in:
info - Creating an optimized production build ..node(10686,0x17548b000) malloc: Incorrect checksum for freed object 0x1191c9c00: probably modified after being freed.
Corrupt value: 0x80f1f1f1
node(10686,0x17548b000) malloc: *** set a breakpoint in malloc_error_break to debug
error Command failed with signal “SIGABRT”.
Interestingly this works just fine in Docker using node 18 alpine base image, but not so on my Mac M1 Ventura 13.4.1 (Darwin 22.5.0). What's more my colleagues have the same issue.
Now I tried switching node version to 20, reinstalling nvm
, using n
, installing node from .pkg but nothing worked. Also I stripped all custom settings in our webpack config, but the issue stayed.
Since malloc hints at the low level C/C++ code, I thought a native dependency in our node_modules might be the culprit. I ran npx native-modules
and found we indeed have one native dep - sharp@0.30.5, which compiles into .node
native node.js addon during yarn install
. When I upgraded it to v0.32.1, the malloc issue went away for me and my colleagues, yey!
Now this is all well, but we don't use sharp during next build
at all. In fact, it's used in an unrelated NPM script as optional task to process images. So why did the upgrade help? Are all node.js addons from node_modules grabbed and linked even though they are not used during an NPM task? I read in the node.js docu that they should be loaded only upon require()
.