You understand what Node.js is, install it on your machine, and run your first JavaScript script outside the browser.
Open this lesson in KodokonUp to now, your JavaScript ran inside a browser (Chrome, Firefox, and so on). To read and run this code, every browser ships with a JavaScript engine: a program that translates your instructions into actions the machine can understand. Chrome's engine is called V8, one of the fastest in the world. In 2009, some developers had a simple idea: take V8 out of the browser. The result is Node.js: a runtime, meaning a program that runs JavaScript anywhere, especially on a server. There is no web page anymore: no document, no window, but in exchange you get access to things the browser forbids, such as the files on your disk and the network. To install it, head to nodejs.org and download the LTS (Long Term Support) version: the version maintained the longest, recommended for beginners. Then open a terminal, the window where you type commands on the keyboard (the "Terminal" app on macOS and Linux, "PowerShell" on Windows).
node --version
npm --versionThe installation gave you three commands. The node command runs JavaScript: followed by a file name, it runs that file; typed on its own, it opens an interactive mode for testing small pieces of code. The npm command (Node Package Manager) is the package manager: it downloads and manages code written by other developers - we'll explore it in the next lesson. Finally, npx runs a tool published on npm without installing it permanently: perfect for trying something just once.
npx cowsay HelloTime for your first script. Create a working folder, open it in your editor (VS Code, for example), and create a file named hello.js. Good news: console.log exists in Node too, but instead of writing to the browser console, it writes directly to the terminal. You'll also discover process, a global object provided by Node that describes the running program: process.version holds the version of Node in use.
console.log("Hello from Node.js!");
console.log("Version:", process.version);node hello.js