Hello readers 👋, welcome to the 1st blog in the NodeJS series!
So far, we've spent a lot of time understanding JavaScript in the browser. Today, we are taking a big step forward. We are leaving the browser behind and stepping into the world of Node.js, a runtime that lets you run JavaScript directly on your computer, just like Python or Ruby.
This is the first post of our Node.js journey. We will start from absolute zero: installing Node.js, checking that it works, playing with the interactive REPL, writing and running a simple script, and finally creating a tiny web server. No frameworks, no abstractions, just raw Node.js. By the end, you'll have a solid foundation to build upon.
Let's get started.
What is Node.js, and why should you care?
Node.js is a JavaScript runtime built on Chrome's V8 engine. What that means is: it takes the same JavaScript engine that powers Google Chrome and places it outside the browser, adding a set of APIs to work with the file system, network, operating system, and more. This allows you to write server-side applications, command-line tools, and scripts using JavaScript, the same language you use for frontend development.
The core idea is that Node.js is event-driven and non-blocking, which makes it perfect for building scalable network applications. But we won't dive into the architectural details just yet. For now, our goal is to install it and run some code.
Installing Node.js
The installation process is straightforward and works similarly across Windows, macOS, and Linux. The easiest way is to download the installer from the official website.
- Go to nodejs.org.
- You'll see two versions: LTS (Long Term Support) and Current. For learning and most projects, choose the LTS version. It's stable and well-tested.
- Download the installer for your operating system.
- Run the installer. Accept the license agreement and keep the default settings (they are fine for beginners). The installer will also set up npm (Node Package Manager) automatically, which we'll use later in this series.
That's it. You now have Node.js on your machine.
Checking the installation using the terminal
To verify everything is installed correctly, open your terminal or command prompt.
- On Windows: you can use Command Prompt, PowerShell, or Windows Terminal.
- On macOS: use Terminal.app (found in Applications > Utilities).
- On Linux: use your preferred terminal emulator.
Type the following commands and press Enter:
node -v
This should print the version of Node.js you installed, something like v20.10.0. If you see a version number, Node.js is ready to go.
Also check npm:
npm -v
This should print the npm version. If both commands give version numbers, the installation was successful.
These commands are OS-neutral. Regardless of your operating system, the terminal works the same way for these checks.
Understanding the Node REPL
REPL stands for Read-Eval-Print Loop. It's an interactive environment where you can type JavaScript code, press Enter, and immediately see the result. Think of it as a live JavaScript scratchpad that runs right in your terminal.
In a browser, you have the console for quick experimentation. Node.js brings that same capability to your computer's command line. It's great for testing short code snippets, exploring language features, or debugging without creating a file.
To start the REPL, open your terminal and simply type:
node
You'll see the prompt change to >. Now you can type JavaScript code and see the output instantly.
Try these lines one by one:
> 2 + 2
4
> console.log("Hello from REPL")
Hello from REPL
undefined
> const name = "Satya"
undefined
> name.toUpperCase()
'SATYA'
> [1, 2, 3].map(x => x * 2)
[ 2, 4, 6 ]
A few things to notice:
- When you type an expression, the REPL prints its value (like
4after2+2). - When you call
console.log, it prints to the output and the REPL showsundefinedbecauseconsole.logreturnsundefined. - Variables persist across lines, just like in a normal script.
To exit the REPL, you can either press Ctrl+C twice, or type .exit and press Enter.
The REPL is a friendly, zero-friction place to test JavaScript. Use it often while learning.
Creating your first JavaScript file
The REPL is great for quick tests, but real applications live in files. Let's create our first Node.js script file.
Using any text editor (Visual Studio Code, Notepad++, Sublime Text, or even a plain Notepad), create a new file and name it app.js. The .js extension tells Node.js that this is a JavaScript file.
Inside app.js, write this simple line:
console.log("Hello from Node.js!");
Save the file in a folder of your choice. Make sure you know the path to that folder because we'll navigate there with the terminal.
Running the script using the node command
Now, open your terminal and navigate to the folder where you saved app.js. For example:
cd path/to/your/folder
Once you are in the correct directory, run the script by typing:
node app.js
You should see the output:
Hello from Node.js!
Congratulations! You have just executed your first Node.js program outside the browser. This is exactly how you'll run every script from now on: write code in a file, save it, and run it with node <filename>.
If your file contains multiple lines, Node runs them sequentially, just like a browser would, but in a pure JavaScript environment with no DOM and no window object.
Writing a Hello World server
Let's now use the built-in http module to create a tiny web server. Node.js comes with several core modules that provide essential functionality without any extra installs. http is one of them.
Create a new file named server.js with the following code:
// Load the http module
const http = require("http");
// Define the server
const server = http.createServer(function (request, response) {
// Set the status code and response headers
response.writeHead(200, { "Content-Type": "text/plain" });
// Write the response body
response.write("Hello World");
// End the response
response.end();
});
// Make the server listen on port 3000
server.listen(3000, function () {
console.log("Server is running at http://localhost:3000");
});
Let's walk through the code:
-
require("http")loads the built-in HTTP module. -
http.createServer()creates a new server instance. The function passed to it is the request handler that gets called every time someone accesses your server. - Inside the handler, we write a simple response: status code 200, content type
text/plain, and the body"Hello World". -
server.listen(3000, callback)starts the server on port 3000 and logs a message when it's ready.
Save the file, then in the terminal run:
node server.js
You'll see the message: Server is running at http://localhost:3000.
Open your browser and visit http://localhost:3000. You should see a plain white page with "Hello World". This is a running web server, built with nothing but Node.js and a handful of lines.
To stop the server, go back to the terminal and press Ctrl+C. That will terminate the Node.js process.
This small example demonstrates the core of how Node.js handles HTTP: you create a server, define what it should do when a request arrives, and start listening on a port. From here, you can return HTML, JSON, handle different routes, and much more. But we'll keep it simple for now; frameworks will come later.
Visualizing the flow
Here is a simplified mental model of what happens when you write and run a Node.js script or server:
- You write JavaScript code in a
.jsfile. - You tell the Node.js runtime to execute that file using the
nodecommand. - Node reads the file, loads required modules, and runs your code in a single-threaded, event-driven environment.
- If your code includes
http.createServer, Node sets up a network listener that continuously watches for incoming requests and calls your handler function for each one. - The output (console logs, network responses) appears in the terminal or in your browser.
The flow can be imagined like this:
[ Your Script (app.js) ]
|
v
Node.js Runtime
|
+---> JavaScript Engine (V8) executes code
+---> Core Modules (http, fs, path, etc.) provide APIs
+---> libuv handles async I/O (file, network)
|
v
Output (console, server response)
This runtime layer is what allows JavaScript to interact with the operating system, something a browser cannot do.
Conclusion
Today, we laid the foundation for our Node.js adventure. We installed Node.js, checked that it works, explored the REPL, wrote and executed a script file, and finally built a tiny Hello World server. These are the first essential steps, and they demystify what Node.js actually does: it takes JavaScript and gives it a powerful backend platform.
To quickly recap:
- Node.js lets you run JavaScript outside the browser, using the V8 engine plus system APIs.
- Installation is straightforward via nodejs.org, and you can check it with
node -vandnpm -v. - The REPL is an interactive playground where you can test code instantly.
- JavaScript files are created and executed with the
nodecommand from the terminal. - The built-in
httpmodule makes it easy to spin up a basic web server in a few lines.
Now that we have Node.js up and running, we can start exploring its core modules, the event loop, and how to build more complex applications. See you in the next post!
Hope you found this helpful! If you spot any mistakes or have suggestions, let me know. You can find me on LinkedIn and X, where I post more about web development.
Top comments (0)