Kodokon kodokon.com

What is Node.js? Install it and run your first script

You understand what Node.js is, install it on your machine, and run your first JavaScript script outside the browser.

8 min · 3 questions

Open this lesson in Kodokon

Up 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).

BASH
node --version
npm --version
Type these two commands in the terminal: if two version numbers appear, the installation succeeded.

The 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.

BASH
npx cowsay Hello
npx downloads the cowsay tool, runs it (a cow drawn in text says "Hello"), then leaves no trace in your project.

Time 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.

JAVASCRIPT
console.log("Hello from Node.js!");
console.log("Version:", process.version);
The contents of hello.js: two lines of ordinary JavaScript.
BASH
node hello.js
From the file's folder, this command runs your script: both messages appear in the terminal.

Knowledge check

Make sure you remember the key points of this lesson.

  1. What is V8?
    • A code editor created by Google
    • Chrome's JavaScript engine, which Node.js reuses
    • The language that replaces JavaScript on servers
    • The terminal installed with Node.js
  2. What is the npx command for?
    • Running a tool published on npm without installing it permanently
    • Updating Node.js to the latest version
    • Automatically creating a package.json file
  3. You've written a file called app.js. Which command runs it?
    • npm app.js
    • node --run app.js
    • node app.js
    • npx node.js app