Introduction to Node.js
Node.js is a runtime that lets you run JavaScript outside the browser to build servers, command-line tools, and back-end applications.
Learn Introduction to Node.js in our free Node.js course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Node.js course at LearnCodingFast — hands-on lessons with examples you run in your browser, 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:
- • A runtime, not a language — you still write JavaScript. Node is just the environment that runs it outside the browser.
- • Built on V8 — the same fast engine that runs JavaScript in Google Chrome compiles your code to native speed.
- • Non-blocking — while one task waits (a file, a network call), the rest of your program keeps running, so a single process can serve many users at once.
- • Batteries included — built-in modules let you read files, build web servers, and handle data without installing anything extra.
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.
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.
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.
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.
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.
Common Errors (and the fix)
- "node: command not found" — Your terminal can't find Node, which usually means it isn't installed (or the terminal was opened before install). Install the LTS version from nodejs.org , then open a new terminal and try node -v again.
- "ReferenceError: window is not defined" — Browser-only objects like window , document , and alert do not exist in Node. Use console.log for output and Node's own globals like process and __dirname instead.
- "Error: Cannot find module './hello.js'" — Node looked for a file that isn't where you said. Check the filename and that your terminal is in the same folder as the file (run ls or dir to confirm it's there).
- Running the wrong filename — node hello (no extension) or a typo in the name produces a "Cannot find module" error. Type the full name including .js : node hello.js .
- "SyntaxError: Unexpected token" — A typo in your JavaScript (a missing quote, bracket, or comma). Read the line number Node prints, fix the punctuation there, and run it again.
Pro Tips
- 💡 Install the LTS version: nodejs.org offers "LTS" (long-term support) and "Current". Pick LTS — it's the stable, well-tested choice for learning and real projects.
- 💡 Try snippets instantly: use a free online runner like onecompiler.com/nodejs to test code without installing anything.
- 💡 Remember where you are: the most common beginner error is running node app.js from the wrong folder. cd into the folder that holds the file first.
📋 Quick Reference — Node.js Basics
Concept
Node Syntax
Run a file
node app.js
Print to the terminal
console.log(x)
Check Node version
node -v
Runtime version (in code)
process.version
Current file's folder
__dirname
The runtime object
process
Frequently Asked Questions
🎉 Lesson Complete!
- ✅ Node.js is a runtime that runs JavaScript outside the browser
- ✅ It's built on the V8 engine that powers Google Chrome
- ✅ Node is non-blocking — it schedules slow work instead of freezing
- ✅ console.log prints to the terminal; process holds runtime info
- ✅ Run a file with node app.js and check your install with node -v
- ✅ Browser-only globals like window don't exist in Node
- ✅ Next lesson: Running Scripts & the REPL — try Node interactively, line by line
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.