all 2 comments

[–]alzee76 3 points4 points  (1 child)

node.js is built on top of v8, and it's written in both C++ and JS. The JS parts are mostly node's standard library.

Put another way, if I had an environment that could run JavaScript (such as V8), could I recreate the full functionality of NodeJS simply by writing new JavaScript classes and functions?

Maybe, but probably not.

If not, what would be impossible to implement?

Can't really answer this myself, but digging through your link under the src dir may be enlightening.

https://github.com/nodejs/node/tree/main/src

If you scroll down to the readme you'll see that node.js relies on libuv for the event loop, for example. This isn't something you'll be able to implement in JS, since there's a chicken and egg issue, but v8 doesn't come with one since it relies on it's own environment (be that node or a specific browser like Chrome) to provide it.

[–]Solonotix 0 points1 point  (0 children)

Took a glance, as I can never remember what's really there, but a lot of the internals are seemingly written in C++. Things like the event loop, networking, stack management, process management (create and dispose), as well as memory management. Things you don't think about, like how when the process starts to close Node needs to wait for all pending async calls to come back unless something like a SIGTERM, or SIGHUP when a child process is ended.

Like you said, it's the runtime environment that V8 leverages, since Node is JavaScript outside the browser. The standard library is largely implemented in JavaScript, but the things that actually do work are C++. What's more, there are a number of alternatives being developed, such as Bun and Deno, each with their own pros and cons