Introduction to Node.js

Reviewed & published by Brayan K

Node.js is a runtime that lets you run JavaScript outside the browser to build servers, command-line tools, and back-end applications.

Part of the free Node.js course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

By the end of this lesson you'll understand what Node.js is and why it exists, how it runs JavaScript without a browser, and you'll have written and run your own first Node script that prints to the terminal.

What You'll Learn in This Lesson

1️⃣ What is Node.js?

For years JavaScript could only run inside a web browser. In 2009 Node.js changed that by taking Chrome's JavaScript engine (called V8) and wrapping it so JavaScript could run anywhere — on servers, on your laptop, in scripts. It rests on a few key ideas:

Because of this, Node powers a huge amount of the modern web — APIs, build tools, and command-line apps. You'll start where every Node developer starts: a tiny script that prints text to the terminal. Read the worked example below first — every line is explained — then run it yourself.

// This is a Node.js script — plain JavaScript that runs OUTSIDE the browser.
// There is no <html> page and no window here; Node gives JS its own runtime.

// console.log writes a line of text to the terminal (standard output).
console.log("Hello from Node.js!");      // -> Hello from Node.js!
console.log("No browser required.");     // -> No browser required.

// You can do real work too — JavaScript math runs exactly as you'd expect.
const total = 2 + 2;
console.log("2 + 2 =", total);           // -> 2 + 2 = 4

2️⃣ Why Node.js? Runtime Info and Non-Blocking Code

Two things make Node feel different from browser JavaScript. First, Node gives you a global process object full of information about the running program — like the Node version and the operating system. Second, Node is non-blocking: code that has to wait gets scheduled and runs later, so your program never freezes while it waits. Read the comments, predict the order of the lines, then check.

// Node runs on Google's V8 engine — the same engine inside Chrome.
// "process" is a global object Node gives you with info about the runtime.

// Two things process can tell you about the machine you are on. They are
// left as comments here because their answers are different on every
// computer — including this page's, which is why no output panel could
// honestly show them. Uncomment them when you run the file yourself:
//   console.log("Node version:", process.version);   // e.g. v22.11.0
//   console.log("Running on:", process.platform);    // linux | darwin | win32

// Node is NON-BLOCKING: slow work (timers, files, network) runs in the
// background while the rest of your code keeps going. Watch the order:
console.log("1. Start");

setTimeout(() => {
  console.log("3. This runs LAST, after 0 ms — it was scheduled, not blocking.");
}, 0);

console.log("2. End of the script body");

Notice the surprise: even with a 0 ms delay, the setTimeout line printed last. Node didn't stop and wait — it scheduled that work and finished the rest of the script first. That scheduling-instead-of-waiting behavior is the heart of why Node handles so many connections efficiently.

Your turn. The script below works once you fill in the two blanks marked ___. Follow the 👉 hints, then run it and compare with the expected output.

// 🎯 YOUR TURN — replace each ___ then run it with: node greet.js

// 1) Store your name in a variable
const name = ___;          // 👉 your name in "double quotes", e.g. "Sam"

// 2) Print a greeting that includes the variable.
//    Template literals use backticks and ${...} to drop a value in.
console.log(`Hello, ${name}! Welcome to Node.js.`);

// 3) Print which version of Node you are running
console.log("You are on Node", ___);   // 👉 the runtime object's .version property
// (hint: it lives on the global "process" object)

// ✅ Expected output (example):
//    Hello, Sam! Welcome to Node.js.
//    You are on Node v20.11.0

3️⃣ Running a File with the node Command

To run a Node script on your own machine, save it in a file ending in .js (for example hello.js) and type node hello.js in your terminal. Node reads the file top to bottom and prints anything you console.log to the screen.

# First, check Node is installed (prints the version number):
node -v

# Save your code in a file called hello.js, then run it:
node hello.js

# Node runs the file and prints whatever you console.log:
# (the lines below are the program's output)

Mini-Challenge: A "Script Info" Report

No blanks this time — just a brief and an outline to keep you on track. Write it yourself, run it with node info.js, and check your output against the example in the comments. This is exactly the kind of tiny program that builds real fluency.

// 🎯 MINI-CHALLENGE: a tiny "about this script" report.
// Save it as info.js and run it with: node info.js
//
// 1. Print a friendly title line of your own.
// 2. Print the Node version using process.version.
// 3. Print the folder this file lives in using the __dirname global.
// 4. Print a small calculation, e.g. how many minutes are in a day.
//    (Use console.log for every line.)
//
// ✅ Example output:
//    === Script Info ===
//    Node version: v20.11.0
//    Folder: /home/sam/projects
//    Minutes in a day: 1440

// your code here

Common Errors (and the fix)

Pro Tips

📋 Quick Reference — Node.js Basics

ConceptNode Syntax
Run a filenode app.js
Print to the terminalconsole.log(x)
Check Node versionnode -v
Runtime version (in code)process.version
Current file's folder__dirname
The runtime objectprocess

🎉 Lesson Complete!

Practice quiz

What exactly is Node.js?

  • A JavaScript framework
  • A runtime that runs JavaScript outside the browser
  • A new programming language
  • A web browser

Answer: A runtime that runs JavaScript outside the browser. Node.js is a runtime; the language is still plain JavaScript.

Which engine does Node.js use to run JavaScript?

  • SpiderMonkey
  • Chakra
  • V8 (the engine in Chrome)
  • Nashorn

Answer: V8 (the engine in Chrome). Node is built on Google's V8 engine, the same one inside Chrome.

How do you run a file named app.js with Node?

  • node app.js
  • run app.js
  • javascript app.js
  • node run app.js

Answer: node app.js. You execute a script by typing node followed by the filename.

Which command prints the installed Node.js version?

  • node version
  • node --show
  • node -v
  • node info

Answer: node -v. node -v (or node --version) prints the version number.

Why is Node.js described as non-blocking?

  • It blocks until each task finishes
  • It only runs one line at a time forever
  • It refuses slow tasks
  • Slow work is scheduled so the rest of the code keeps running

Answer: Slow work is scheduled so the rest of the code keeps running. Node schedules slow work and keeps going instead of freezing.

Which global object holds runtime info like the Node version?

  • window
  • process
  • document
  • global.info

Answer: process. The global process object exposes details such as process.version.

What happens if you use window or document in Node?

  • They print a warning then work
  • They are the same as in the browser
  • They throw a ReferenceError — they do not exist in Node
  • They open a browser window

Answer: They throw a ReferenceError — they do not exist in Node. Browser-only globals like window and document do not exist in Node.

What does console.log do in a Node script?

  • Opens a dialog box
  • Saves to a database
  • Sends an HTTP request
  • Writes a line of text to the terminal

Answer: Writes a line of text to the terminal. console.log writes to standard output (the terminal).

In what year did Node.js first let JavaScript run outside the browser?

  • 2009
  • 1999
  • 2015
  • 2020

Answer: 2009. Node.js was released in 2009, bringing JavaScript to servers.

When installing Node from nodejs.org, which build is recommended for learning?

  • The Nightly build
  • The LTS (long-term support) version
  • The Current version
  • The Beta version

Answer: The LTS (long-term support) version. LTS is the stable, well-tested choice for learning and production.

Continue this course

Frequently asked questions

What exactly is Node.js — a language, a framework, or something else?

Node.js is none of those: it's a runtime. The language is still JavaScript. A web browser is one runtime that can execute JavaScript (and gives it the window and document objects); Node.js is a different runtime that runs the same JavaScript on a server or your own machine, and gives it server-style abilities instead — reading files, opening network connections, and reading command-line input. It's built on Chrome's V8 engine, which is what actually compiles and runs your JavaScript.

Do I need to know front-end JavaScript before learning Node.js?

Not deeply. You should be comfortable with core JavaScript — variables, functions, objects, arrays, and async basics like callbacks or promises — but you do not need React, the DOM, or any browser knowledge. In fact, browser-only things such as window, document, and alert do not exist in Node and will throw a ReferenceError if you use them. Node focuses on the language itself plus its own built-in modules.

Why is Node.js described as non-blocking or asynchronous?

Most slow operations in a program involve waiting — for a file to load, a database to answer, or a network request to return. Node is designed so that while one piece of code is waiting, the rest of your program keeps running instead of freezing. It does this with an event loop and callbacks (or promises and async/await). That's why a single Node process can handle thousands of simultaneous connections efficiently, which makes it popular for servers and APIs.

How do I actually run a Node.js file?

Install Node.js from nodejs.org (the LTS version is the safe choice), then save your code in a file ending in .js — for example app.js. Open a terminal in that folder and type node app.js. Node executes the file top to bottom and prints anything you console.log to the terminal. You can check your install worked by running node -v, which prints the version number.