[libnode] How to build nodejs/libnode to a statically linkable binary? #4560
-
Hi all, I am trying to build libnode such that it produces a statically linkable library I can embed within my application. It's okay for system dependencies that the Nodejs CLI links dynamically to remain dynamic (like openssl) - I don't need them to be statically included because I am essentially recreating the Nodejs CLI. I want to produce libraries for I have tried several approaches without success, namely: ./configure --shared --fully-static --enable-static
make -j16 # Patch node.gyp to build in static
sed -i "s/'type': 'executable',/'type': 'static_library',/g" ./node.gyp
./configure
make -j16 And combinations of the flags/patches # Patch node.gyp to build in static
sed -i "s/'type': 'executable',/'type': 'static_library',/g" ./node.gyp
./configure --shared --fully-static --enable-static
make -j16 I have tried this on Fedora 41 without success so I also tried running these commands within docker containers: COMMANDS=$(cat <<-END
set -e
cd /workdir
apt update -y
apt upgrade -y
apt install -y software-properties-common wget curl jq git build-essential zsh make cmake jq clang
git config --global safe.directory '*'
git clone "https://github.com/nodejs/node" --branch v23.11.0 --depth=1 ./node
cd ./node
./configure --shared --fully-static --enable-static
make -j16
END )
# Have tried ubuntu:22.04 ubuntu:24.04 debian:12, fedora:41
sudo docker run --rm -it -v $PWD/workdir:/workdir debian:12 /bin/bash -c "echo -e \"$COMMANDS\" | /bin/bash -s" Once I figure it out I will repeat these steps on MacOS and Windows. References |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hey, I totally get the struggle Building a statically linkable libnode isn’t officially supported by Node.js, and it’s notoriously tricky. The build system is really set up for producing the CLI (the node binary) rather than a reusable static library. Most folks who have managed something similar usually end up maintaining their own fork or heavy patch set, and even then, keeping up with Node releases can be a pain. That is why tools like metacall/libnode exist. They do a ton of patching/automation behind the scenes. If you absolutely need static linking, you might have to go down the rabbit hole of forking and customizing the build (as you started). Otherwise, using dynamic linking with the shared library is much more straightforward and supported. Sorry there isn't a smoother answer. It’s a bit of a rabbit hole! If you are okay with dynamic linking for your use case, I would recommend going that route for your sanity. Good luck. This is not an easy one! If you make any progress, I’d love to hear about it. |
Beta Was this translation helpful? Give feedback.
Hey, I totally get the struggle
Building a statically linkable libnode isn’t officially supported by Node.js, and it’s notoriously tricky. The build system is really set up for producing the CLI (the node binary) rather than a reusable static library.
Most folks who have managed something similar usually end up maintaining their own fork or heavy patch set, and even then, keeping up with Node releases can be a pain. That is why tools like metacall/libnode exist. They do a ton of patching/automation behind the scenes.
If you absolutely need static linking, you might have to go down the rabbit hole of forking and customizing the build (as you started). Otherwise, using dynamic linking with …