Setting Up — Installing Node.js
Every journey starts with a first step — and for us, that step is installing Node.js. Think of it like installing a new language on your computer. Once installed, your machine will understand JavaScript not just in the browser, but everywhere.
- Download Node.js. Head to
https://nodejs.organd download the LTS (Long Term Support) version. LTS is recommended for most users because it’s stable and receives security updates for longer. The “Current” version has the latest features but may be less stable. - Run the installer. Follow the installation wizard. It will install two things: Node.js (the runtime) and NPM (the package manager). Both are essential. Accept the default settings — they work perfectly for beginners.
- Verify the installation. Open your terminal (Command Prompt on Windows, Terminal on Mac/Linux) and run the verification commands below. If you see version numbers, you’re all set.
# Check Node.js version
$ node -v
v20.15.0
# Check NPM version
$ npm -v
10.7.0
# If you see "command not found", Node.js is NOT installedThe Playground — Node REPL
Now that Node.js is installed, you have a secret playground available right in your terminal. It’s called the REPL — and it’s one of the fastest ways to experiment with JavaScript outside a browser.
| Step | What it does |
|---|---|
| Read | Takes your input |
| Evaluate | Executes the line via V8 |
| Shows the return value | |
| Loop | Waits for your next line |
To enter the REPL, simply type node in your terminal and press Enter.
You’ll see a > prompt — that’s the REPL waiting for your JavaScript.
It’s similar to the browser’s developer console, but powered by Node.js
instead.
$ node
Welcome to Node.js v20.15.0.
Type ".help" for more information.
> 1 + 1
2
> let a = "jatin";
undefined
> let b = "sharma";
undefined
> a + b
'jatinsharma'
// Press Ctrl+C twice or type .exit to leave the REPLThe REPL is great for quick experiments, but it’s not practical for real projects. You lose all your code when you close the terminal. That’s why, for actual development, we write code in files.
Real Code — Files & VS Code
The REPL is a playground, but your real workshop is a code editor. VS Code (Visual Studio Code) is the most popular choice for Node.js development — it’s free, powerful, and has excellent JavaScript support built in.
- Create a project folder (e.g.,
my-nodejs-project). This is where all your project files will live. - Open it in VS Code. Launch VS Code and go to File → Open Folder. Select your new folder. This sets up your workspace.
- Create
app.js. In the VS Code file explorer, create a new file calledapp.js. This is the conventional entry point for a Node.js application. - Write & run. Write your JavaScript code in the file, open the
integrated terminal with
Ctrl +`, and run it withnode app.js.
// app.js
let name = "Node JS 03";
let a = 5;
let b = 10;
let c = a + b;
console.log(name); // "Node JS 03"
console.log(c); // 15$ node app.js
Node JS 03
15And just like that, you’ve written JavaScript that runs outside any
browser. No HTML file, no <script> tag, no web page needed. Just a
.js file and the node command. This is the Node.js runtime
environment in action — using V8 behind the scenes.
| Mode | How to invoke | Behaviour | Best for |
|---|---|---|---|
| REPL | node | Interactive, one line at a time | Quick experiments |
| File | node app.js | Runs the entire file at once | Real projects |
The REPL is great when you want to poke at a single expression. File mode is where real work happens: code persists, gets committed, and can be shared with the rest of your team.
The Hidden World — Global Objects
If you’ve used JavaScript in a browser, you know about the window
object — the all-powerful global object that gives you access to
alert(), document, localStorage, and more. But here’s the thing:
the window object is provided by the browser, not by V8.
So what happens in Node.js, where there is no browser and no window?
Node.js provides its own global object, simply called global.
| Feature | Browser | Node.js |
|---|---|---|
| Global object | window | global |
| DOM access | document, getElementById | Not available (no DOM) |
| Timers | setTimeout, setInterval | setTimeout, setInterval, setImmediate |
| Console | console.log | console.log |
| Fetch API | Built-in | Built-in (since Node 18+) |
| File system | Not available | require('fs') |
| Alert / Prompt | alert(), prompt() | Not available (no UI) |
| Universal reference | globalThis (works in both) | globalThis (works in both) |
The this Mystery & globalThis
Here’s a surprise that trips up many developers. If you type
console.log(this) at the top level of a Node.js file, you might expect
it to log the global object. But instead, you get:
console.log(this); // Outputs: {}
// Wait... an empty object? Not `global`?Why? Because in Node.js, each file is treated as a module. At the top
level of a module, this refers to module.exports, which starts as an
empty object {}. This is different from the browser, where top-level
this refers to window.
To solve the confusion across environments, ECMAScript 2020 introduced
globalThis — a universal reference to the global object that works
everywhere:
- In browsers:
globalThis === window. They point to the same object. You can use either one. - In Node.js:
globalThis === global. They point to the same object. Both give you access to timers, console, and so on. - Why it matters:
globalThisprovides a consistent way to access the global object without worrying about which environment your code runs in. Write once, run anywhere.
console.log(globalThis);
// Output: Object [global] {
// global: [Circular *1],
// clearImmediate: [Function],
// setImmediate: [Function],
// clearInterval: [Function],
// clearTimeout: [Function],
// setInterval: [Function],
// setTimeout: [Function],
// queueMicrotask: [Function],
// fetch: [Function],
// crypto: [Getter],
// performance: [Getter/Setter],
// ... }Episode 03 — At a Glance
| Concept | Key detail |
|---|---|
| Install Node.js | Download LTS from nodejs.org; comes with NPM. |
| Verify install | node -v and npm -v in terminal. |
| REPL | Read-Evaluate-Print-Loop — type node to enter, .exit to leave. |
| Run a file | node app.js — executes the entire file. |
| VS Code setup | Create folder → Open in VS Code → Create app.js → Ctrl+` . |
| Browser global | window — provides DOM, alert, localStorage, etc. |
| Node.js global | global — provides setTimeout, setInterval, console, fetch. |
this at top level | In Node.js files: {} (module.exports) — not global. |
globalThis | Universal reference (ES2020) — window in browsers, global in Node. |
| Who provides global? | Node.js provides global; the browser provides window. Not V8. |
Comments
Comments are disabled in this environment. Set
PUBLIC_GISCUS_REPO,PUBLIC_GISCUS_REPO_ID, andPUBLIC_GISCUS_CATEGORY_IDto enable.