nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
105.76k stars 28.7k forks source link

Allow for slim node builds #51928

Open samardzicneo opened 5 months ago

samardzicneo commented 5 months ago

What is the problem this feature will solve?

When building Nodejs as a static library, it might be pointless to include all core modules in the resulting binary. Modules such as crypto, http, http2, http3, https, tls, tcp, udp, etc. all increase the binary size but might not be ever used.

What is the feature you are proposing to solve the problem?

Add build-time flags to enable or disable core modules.

What alternatives have you considered?

Just shipping the fat libnode dylib works fine but it's 90MB+ and that could be cut down pretty far by skipping unused core modules.

marco-ippolito commented 5 months ago

You can already do that: for example you can run ./configure --without-npm

 --without-npm         do not install the bundled npm (package manager)
 --without-corepack    do not install the bundled Corepack
 --without-ssl         build without SSL (disables crypto, https, inspector, etc.)
 --without-node-options

Run ./configure --help to see all the flags

samardzicneo commented 5 months ago

You can already do that: for example you can run ./configure --without-npm

 --without-npm         do not install the bundled npm (package manager)
 --without-corepack    do not install the bundled Corepack
 --without-ssl         build without SSL (disables crypto, https, inspector, etc.)
 --without-node-options

Run ./configure --help to see all the flags

Thanks for mentioning those, I'm aware that they exist. Unfortunately, those are basically the only ones available. There is no way to disable http, http2, https, quic, tcp, udp and most other core modules, or, say, compile without the REPL if that makes a difference.

marco-ippolito commented 5 months ago

I would be +1 for the addition if someone is interested in sending a PR

samardzicneo commented 4 months ago

@marco-ippolito Unfortunately I'm not at all proficient in C/C++ and don't think I could implement such a thing without making a mess, but I'm gonna try to put something together anyways just to check the feasability and effectiveness of it. If I come up with anything, I'll post an update 👍🏼

RedYetiDev commented 3 months ago

I'm not even sure it would be possible. That would require modularizing many of the Node.js internals, which is a huge change.

aifrim commented 1 month ago

But it would be awesome

I'm not even sure it would be possible. That would require modularizing many of the Node.js internals, which is a huge change.

RedYetiDev commented 1 month ago

Maybe so, and if you're willing to make a proposal/idea, go ahead, but it's no easy feat.

RedYetiDev commented 1 month ago

FWIW I've opened a proposal, and I'd love feedback on it.

“Everything is theoretically impossible, until it is done.” – Robert A. Heinlein

tniessen commented 1 month ago

Add build-time flags to enable or disable core modules.

One of the main challenges with such an approach is that some internal modules depend on each other. Even if we were able to account for the transitive results of disabling one module for arbitrary core modules, we would then have an exponential number of configurations to support.

With some other languages, the best approach is often to compile everything and to then remove unused code during the linker phase. However, with JavaScript, this is much trickier than with, say, C/C++.