Node.js Basics: JavaScript Outside the Browser
What Is Node.js?
In this lesson you will
- Explain what Node.js is and what it is used for
- Run a JavaScript file with Node
- Read command-line arguments and use the REPL
Introduction
JavaScript began as a language for web browsers. Node.js lets you run JavaScript outside the browser, on your own computer or on a server. That means you can use one language to build websites, command-line tools, APIs and much more. This course assumes you know basic JavaScript. The code is shown for reading, and the outputs come from real runs with Node 24.
The simple explanation
Node.js is a program that reads your JavaScript file and runs it, with extra tools for files, networks and the operating system.
Browsers give JavaScript things like document and window. Node does not have those. Instead it gives you:
- Files and folders (
fs) - Networking and web servers (
http) - Operating system information (
os,process) - Cryptography (
crypto) and much more
It runs on the same V8 engine that powers Chrome, so the language itself is the same.
Install and check
Download the current LTS (long-term support) version from the official Node.js website, or use your system's package manager. Then in a terminal:
Terminal
node --version
npm --versionYou will see version numbers. LTS versions are the safest choice for real projects.
Your first script
Create a file named hello.js:
Run it:
Terminal
node hello.jsHello from Node!
5There is no browser and no HTML. The output goes to your terminal.
Quick quiz
What is Node.js?
Command-line arguments
process.argv is an array of the words you typed. The first two items are Node and the script, so your arguments start at index 2:
Running node greet.js Ada Lovelace prints:
You typed: [ 'Ada', 'Lovelace' ]The REPL
Type just node and press Enter to open the REPL (read, evaluate, print loop), an interactive prompt:
> 2 + 3
5
> [1, 2, 3].map((n) => n * 2)
[ 2, 4, 6 ]
> .exitIt is perfect for trying small ideas. Type .exit or press Ctrl+D to leave.
What people build with Node
- Web servers and APIs, which you will build in this course
- Command-line tools, such as build scripts
- Real-time apps, such as chat
- Tools for front-end developers, such as bundlers and test runners
Node is good at work that waits for things, such as files and networks, because of how it handles asynchronous code. You will see that in the third lesson.
Try it yourself
Write a script that prints the arguments you give it in upper case, joined with commas. Run it with node shout.js one two three.
Summary
- Node.js runs JavaScript outside the browser, with built-in modules for files, networking and the system.
- Run a script with
node file.js, read arguments fromprocess.argv, and experiment in the REPL. - Choose an LTS version for real projects.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading