Get Started

Getting Started with Node.js

Welcome to the world of backend JavaScript! Node.js is an open-source, cross-platform runtime environment that allows you to execute JavaScript code outside of a web browser. It is widely used for building fast, scalable, and highly available web applications.

In this tutorial, you will learn how to get started with Node.js, install it on your computer, and write your very first Node.js application.


1. What is Node.js?

Historically, JavaScript was only used inside web browsers to make web pages interactive. Node.js changed everything. By wrapping Google Chrome's extremely fast V8 JavaScript Engine inside a C++ program, Node.js allows JavaScript to talk to your computer's operating system.

This means you can now use JavaScript to:

Node.js is asynchronous and event-driven, which makes it incredibly efficient for handling thousands of simultaneous connections.


2. Installing Node.js

To run Node.js on your machine, you need to download and install it.

  1. Visit the official Node.js website at nodejs.org.
  2. You will see two download options:
    • LTS (Long Term Support): Recommended for most users. This version is stable and guaranteed to be supported for a long time.
    • Current: Contains the newest features, but might contain experimental changes.
  3. Download the LTS version and run the installer. The default settings are perfectly fine for beginners.

Verify Your Installation

Once installed, open your computer's terminal (Command Prompt or PowerShell on Windows, Terminal on macOS/Linux) and type the following command to check the installed version:

node -v

If Node.js is installed correctly, it will print out the version number (for example, v20.9.0).

Node.js also comes with npm (Node Package Manager). You can verify it by typing:

npm -v

3. Your First Node.js Program

Let's write a simple program. You don't need a browser for this; we will run it directly from the terminal.

  1. Create a new folder on your computer named node-practice.
  2. Open that folder in your favorite code editor (like Visual Studio Code).
  3. Create a new file named app.js.
  4. Add the following JavaScript code to app.js:

app.js

// Output a welcome message to the console
console.log("Hello, Node.js World!");

// Perform a simple calculation let a = 10; let b = 25; console.log("The sum of a and b is:", a + b);

Running the Code

To execute this file, open your terminal, navigate to the node-practice folder, and tell Node to run your file by typing:

node app.js

Output:

Hello, Node.js World!
The sum of a and b is: 35

Congratulations! You have just written and executed your very first Node.js program.


4. The Node.js REPL

If you just want to test a quick line of JavaScript without creating a file, Node.js provides a built-in environment called the REPL (Read-Eval-Print Loop).

Open your terminal and type node and press Enter. Your terminal prompt will change to >. You are now inside the Node environment!

Try typing standard JavaScript:

> 5 + 5
10
> Math.random()
0.84920138402
> ["Node", "React", "Vue"].length
3

To exit the REPL, type .exit and press Enter, or press Ctrl + C twice.


Next Steps

Now that you have Node.js installed and running, the next step is to ensure you have a solid grasp of the JavaScript features that power modern Node.js applications. In the next tutorial, we will review the essential JavaScript requirements you need before diving deeper.


Exercise

?

Which command is used to execute a Node.js file named server.js in the terminal?