Time to get hands-on. In this chapter you'll install Node.js, play with it interactively, and run your first JavaScript file outside the browser.
In this chapter you'll learn
- how to install Node.js (and which version to pick)
- how to check that Node.js and npm are installed
- what the REPL is and how to use it
- how to write and run a
.jsfile from the terminal and from VS Code - a few things that are different from running JavaScript in the browser
Before you start: read Chapter 01: Introduction to NodeJs and Chapter 02: JS on the Server. The example file for this chapter is in Code/app.js.
Step 1: Install Node.js
Pick a version: LTS vs Current
On nodejs.org you'll see two kinds of release:
- LTS (Long-Term Support): stable, gets security fixes for about three years. Choose this.
- Current: the newest features, but changes often. Good for experimenting, not for real projects.
Installing Node.js also installs npm, the package manager you'll use from Season 2.
Option A: the official installer (simplest)
- Go to nodejs.org and download the LTS installer for your operating system.
- Run it and accept the defaults. On Windows, keep "Add to PATH" checked. PATH is the list of folders your terminal searches for commands; without it, typing
nodegives "command not found". - Close and reopen your terminal so it picks up the new PATH.
Option B: a version manager (recommended once you're comfortable)
Real projects often need different Node.js versions. A version manager lets you install several and switch between them:
- macOS / Linux: nvm
- Windows: nvm-windows or fnm
nvm install --lts # install the latest LTS
nvm use --lts # switch to itnvm install --lts # install the latest LTS
nvm use --lts # switch to itStep 2: Check the installation
Open a terminal (Command Prompt or PowerShell on Windows, Terminal on macOS/Linux) and run:
node -v
npm -vnode -v
npm -vYou should see version numbers, such as v24.11.0 and 11.6.2. If you get "command not found", reopen the terminal; if it still fails, the PATH step didn't work, so reinstall and check the PATH option.
Step 3: Try the REPL
Type node with nothing after it and press Enter:
nodenodeYou're now in the REPL: Read, Evaluate, Print, Loop. It reads what you type, runs it, prints the result, and waits for the next line. It's like the browser's console, but in your terminal.
> 2 + 3
5
> const name = "Akshay"
undefined
> `Namaste ${name}`
'Namaste Akshay'
> typeof window
'undefined'
> typeof global
'object'> 2 + 3
5
> const name = "Akshay"
undefined
> `Namaste ${name}`
'Namaste Akshay'
> typeof window
'undefined'
> typeof global
'object'Notice the last two lines: there's no window in Node.js (no browser), but there is a global object. The standard name that works everywhere is globalThis.
Useful REPL commands:
| Command | What it does |
|---|---|
.help | list all REPL commands |
.editor | write several lines, then press Ctrl + D to run them |
_ | the result of the last expression |
Ctrl + C twice, or .exit | leave the REPL |
The REPL is great for quickly testing a small idea, like "what does [1,2,3].at(-1) return?". For anything longer than a few lines, use a file.
Step 4: Write and run a file
In VS Code
- Install Visual Studio Code.
- Create a folder for this course and open it in VS Code (File → Open Folder).
- Create a file called
app.jswith this code:
// app.js
const name = "Namaste Node.js";
console.log(`Hello from ${name}!`);
const a = 10;
const b = 20;
console.log("Sum is", a + b);// app.js
const name = "Namaste Node.js";
console.log(`Hello from ${name}!`);
const a = 10;
const b = 20;
console.log("Sum is", a + b);- Open the built-in terminal: View → Terminal, or
Ctrl + `(the backtick key, left of1). - Run the file:
node app.jsnode app.jsOutput:
Hello from Namaste Node.js!
Sum is 30Hello from Namaste Node.js!
Sum is 30node app.js means: "Node.js, run the JavaScript in the file app.js." The .js extension is optional (node app also works), but writing it is clearer.
Run the command from the folder that contains the file. If you get Error: Cannot find module '.../app.js', your terminal is in a different folder. Use cd to move into the right one (cd path/to/folder), or ls / dir to see what's there.
From the terminal only
You can also create the file from the terminal. The command depends on your shell:
# macOS / Linux
echo 'console.log("Hello, World!")' > app.js
# Windows Command Prompt (cmd)
echo console.log("Hello, World!") > app.js
# Windows PowerShell
Set-Content app.js 'console.log("Hello, World!")'# macOS / Linux
echo 'console.log("Hello, World!")' > app.js
# Windows Command Prompt (cmd)
echo console.log("Hello, World!") > app.js
# Windows PowerShell
Set-Content app.js 'console.log("Hello, World!")'Then run it with node app.js.
In Windows PowerShell 5, echo ... > app.js saves the file in UTF-16 encoding, and Node.js fails with SyntaxError: Invalid or unexpected token. Use Set-Content as shown above.
Step 5: Rerun automatically while you code
Running node app.js after every change gets tiring. Node.js (version 18.11 and newer) can watch the file and rerun it whenever you save:
node --watch app.jsnode --watch app.jsIn Season 2 you'll also meet nodemon, a popular tool that does the same for whole projects (Chapter S2 03: Creating our Express server).
A few differences from browser JavaScript
| In the browser | In Node.js |
|---|---|
window, document, alert() exist | they don't exist; using them throws ReferenceError |
global object is window | global object is global (globalThis works in both) |
this at the top of a script is window | in a CommonJS file, this at the top is module.exports (an empty object {}); in an ES Module it's undefined. You'll see why in Chapter 05: Diving in to NodeJS github repo |
one page = many <script> tags sharing globals | each file is a separate module with its own scope, using require/module.exports or import/export (Chapter 04: module.export & require) |
Things that work in both: console.log, setTimeout, setInterval, fetch (Node.js 18+), JSON, Promise, and the rest of the language itself.
Common mistakes
- Running
nodewith the wrong folder open. Check where you are withpwd(macOS/Linux) orcd(Windows). - Typing JavaScript into the normal terminal instead of the REPL or a file.
console.log("hi")at a$prompt is a shell error, not a Node.js error. - Using browser APIs like
document.getElementByIdin Node.js. There's no web page on a server. - Forgetting to save the file before running it. VS Code shows a dot on unsaved tabs.
Key takeaways
- Install the LTS version of Node.js. npm comes with it.
- Check with
node -vandnpm -v. nodealone opens the REPL for quick experiments.node file.jsruns a file;node --watch file.jsreruns it on every save.- There is no
windowordocumentin Node.js. The global object isglobal(orglobalThis).
Interview questions
Q1. What is the REPL in Node.js?
REPL stands for Read–Evaluate–Print–Loop. It's an interactive shell you start by typing node. It reads an expression, evaluates it, prints the result, and loops. It's used for quick experiments and debugging, and supports commands like .help, .editor and _ (the last result).
Q2. How do you run a JavaScript file with Node.js?
Open a terminal in the file's folder and run node fileName.js. Use node --watch fileName.js (Node 18.11+) to rerun it automatically on every save.
Q3. What's the global object in Node.js?
It's global. It holds things available everywhere, like setTimeout, console and process. In the browser the global object is window. globalThis is the standard name that works in both environments.
Q4. What is the difference between LTS and Current releases?
Current releases have the newest features and change often. Every even-numbered release becomes LTS (Long-Term Support) and gets bug and security fixes for about three years. Production apps should run on an active LTS version.
Q5. Why would you use a version manager like nvm?
Different projects may need different Node.js versions. A version manager lets you install several versions side by side and switch per project (nvm use 22). It also avoids permission problems that global installs sometimes cause.
Q6. What is process in Node.js?
process is a global object with information about, and control over, the running Node.js process. Common uses: process.env (environment variables, see Chapter S2 06: Database, Schema, Models & Mongoose), process.argv (command-line arguments), process.exit() and process.cwd() (current folder).
What's next
So far your code lives in a single file. Real projects are split across many files. In Chapter 04: module.export & require you'll learn how files share code with import / export (the modern standard, also used in React), and how to read the older require / module.exports style.