In several languages, instead of returning something like "macOS", the command to view the operating system instead returns
I see this in languages like R, and node.
Why is macOS sometimes called Darwin??
Why is macOS often referred to as 'Darwin'?
It isn't. macOS isn't Darwin and Darwin isn't macOS.
The history of macOS is long, convoluted, and complicated.
It starts with Steve Jobs (not entirely voluntary) "leaving" Apple and founding NeXT. NeXT wanted to revolutionize the Personal Workstation. They built both a powerful computer, the NeXT Computer (later NeXTstation and NeXTcube), and a powerful, modern Operating System, called NeXTStep. (Get it? The next step for the next computer. Really creative naming.)
The Operating System was based on porting BSD to a Mach microkernel, and adding object-oriented system libraries, frameworks, and toolkits (called "kits", which you can still see in Apple's naming today), with an object-oriented GUI framework and desktop, with object-oriented applications, all written in a modern object-oriented programming language (Objective-C) as the systems language, on top of the base BSD system. The display system was based on PostScript, and there was even an Intel i860 coprocessor running a stripped-down version of the OS, only for Display PostScript processing, plus a powerful DSP for video and audio processing.
NeXTStep pioneered many things we see in modern GUI programming. It had one of the first graphical GUI Builders, which still to this day is how you design GUIs for macOS, iOS, iPadOS, etc. (Today, it is called the Xcode Interface Builder.) It had the first ever App Store. When Tim Berners-Lee invented the World Wide Web, he chose NeXTStep as the OS to write the first browser for. Many game studios used NeXTStep and NeXT workstations for their development, e.g. id software for Doom, Doom 2, and Quake. Lotus Improv, still considered by many to be miles ahead of Excel even now, was implemented on NeXTStep.
Later, NeXT divorced the higher-level frameworks from the underlying OS and made them available under the name OpenStep for Windows NT, Sun Solaris, and under the name "OPENSTEP for Mach" still based on the same underpinnings as the original NeXTStep.
At this point, Apple had tried and failed multiple times to modernize MacOS, and they bought NeXT (thus bringing Steve Jobs back into the company) and all of its Intellectual Property and technology to develop a successor to MacOS based on OPENSTEP for Mach. They modernized the Mach kernel from 2.5 to 3 and extended it with concepts from the FreeBSD kernel to form a kernel known as xnu (a reference to the failed nuKernel project at Apple which was to develop a "new kernel"), and the BSD underpinnings from 4.3BSD to 4.4BSD and later FreeBSD.
Most importantly, they extended and expanded the OpenStep APIs and built new APIs on top. The collection of those APIs is known as "Cocoa". They also built an API called "Carbon", which was a close, but not identical re-implementation of a subset of the MacOS API on top of the new foundations. (The intention was that while it would not be possible to simply re-compile existing MacOS applications, it should be fairly easy to port them to Carbon, and then over the years rewrite them in Cocoa.)
The first prototype of this system was called Rhapsody. The full system wasn't finished in time, so a subset was released as MacOS X Server 1.0. And the rest is history: Rhapsody became MacOS X, then OS X, then macOS, and somewhere along the way, iOS was split off, and then further divided into iOS, iPadOS, tvOS, and watchOS.
Now, back to Darwin: Darwin is basically the underpinnings of macOS, from the xnu kernel, IOKit, drivers, etc. up to the BSD libraries and userland, plus some macOS-specific developments such as mDNSresponder and launchd. It does not, however, include any parts of what used to be OpenStep, Cocoa, Aqua, Quartz, QuickTime, or any of the other higher-level stuff. It does contain drivers and filesystems, although I am not entirely sure whether APFS is part of Darwin.
If you think back to the point in time where NeXT "divorced" the high-level OpenStep from its underpinnings, the low-level parts that are not OpenStep would be the ones that would later become Darwin.
In the beginning, Apple used to make Darwin available as a separate OS, including compiled binaries, installers, ISOs, etc. that you could install on Apple hardware. However, for many years now, Apple only provides a source code dump, every time a new release of macOS comes out. It isn't even possible to compile this source code, because it depends on Apple's internal build tools and build pipeline. There have been some projects trying to patch Darwin to compile it with publicly available tools, but those projects have all died from lack of interest.
Since all of the things you mentioned were born on Unix and use Unix APIs and Unix libraries, they actually typically don't even know about the "non-Darwin" parts of macOS, so it is only logical that they will consider the OS to be "Darwin". Note that "Darwin" is also what gets returned as the name of the OS when you call the Unix/POSIX
int uname(struct utsname *buf) library function or the
uname Unix/POSIX commandline utility.
So, to answer the question you didn't ask explicitly but is implicit in your question: why does Node.js return "Darwin" for the name of macOS? Because when Node.js asks macOS for its name, that's what macOS tells it its name is!
Darwin is the core upon which macOS (previously Mac OS X, and OS X) runs on.
It is derived from NextSTEP, which itself is built upon a BSD and Mach core, but Darwin is the open source portion of macOS.
Historically, when Apple bought NeXT in the late 1990s, they built upon the NeXT operating system (NeXTSTEP) to form the basis of the next version of the OS for the Mac.
See Wikipedia's Darwin entry for more details.
There are lots of ways to fingerprint an OS, but a canonical method for this on unix-like systems is to call
uname Which outputs Darwin on all macOS versions and builds.
Apple publishes the source code to the Darwin part of the OS so you can inspect the code that matches the name.
If you want to see this yourself, open a shell (in Terminal.app) and you can generate the Darwin string and read the manual page for
uname command or the system call (man 3)
man 3 uname
Here's a nice example of a program needing to know which system is running by calling
uname from a shell script. It then adjusts syntax and variables to do the same task on various OS:
I'll leave the "Why did human beings do this" for another time - that story is rich and multifaceted and there's tons of stories documented publicly on the Internet for the curious.
From Wikipedia, Darwin is an open-source Unix-like operating system first released by Apple Inc. in 2000. It is composed of code developed by Apple, as well as code derived from NeXTSTEP, BSD, Mach, and other free software projects.
Here is a quote from the book Take Control of the Mac Command Line with Terminal
When Apple bought NeXT in 1996, they began building a new operating system that extended and enhanced NeXTSTEP while layering on capabilities (and some of the user interface) of the classic Mac OS. The result was Mac OS X (later renamed OS X and now macOS): it’s Unix underneath, but with lots of extra stuff that’s not in other versions of Unix. If you took macOS and stripped off the graphical interface, the Cocoa application programming interfaces (APIs), and all the built-in apps such as Mail and Safari, you’d get the Unix core of macOS. This core has its own name: Darwin. When you work in the command-line environment, you’ll encounter this term from time to time.