Skip to main content

Command Palette

Search for a command to run...

Setting Up Your First Node.js Application Step-by-Step

Updated
3 min read

JavaScript was once confined to the browser, used purely to make web pages interactive. That changed with Node.js. By taking the V8 JavaScript engine out of the browser, Node.js allows you to run JavaScript directly on your machine, opening the door to building robust backend services, command-line tools, and full-scale applications.

If you are just getting started with backend development, setting up your first Node.js environment is a fundamental milestone. Here is a step-by-step guide to getting your first application up and running.

Installing Node.js

Regardless of whether you are running Windows, macOS, or a Linux distribution, the installation process is straightforward.

  1. Head over to the official Node.js website.

  2. You will typically see two download options: LTS (Long Term Support) and Current.

  3. Always choose the LTS version. It provides the most stable foundation for development and avoids the bleeding-edge bugs that can sometimes accompany the "Current" releases.

  4. Download the installer for your specific operating system and run it. The default installation settings are perfectly fine for most setups.

Checking the Installation

Once the installation is complete, you need to verify that your operating system recognizes Node.js. Open your terminal or command prompt and type the following command:

node -v

If the installation was successful, this command will return the version number (e.g., v20.11.0). It is also good practice to check if Node Package Manager (npm) was installed alongside it, as it comes bundled with Node.js:

npm -v

Understanding the Node REPL

Before writing full scripts, it is helpful to understand the Node REPL. REPL stands for Read, Eval, Print, Loop. It is an interactive, virtual environment—like a sandbox—where you can write JavaScript code directly in your terminal and see the results instantly.

  • Read: It reads your input.

  • Eval: It evaluates the JavaScript code.

  • Print: It prints the result to the console.

  • Loop: It waits for your next command.

To enter the REPL, simply type node in your terminal and press Enter. The prompt will change to a > character. Try typing a simple math operation or a string manipulation. To exit the REPL environment, press Ctrl + C twice, or type .exit.

> 5 + 10
15
> "Hello" + " World"
'Hello World'

Creating Your First JS File

While the REPL is great for quick tests, real applications are written in files. Let's create your first Node.js script.

Create a new directory for your project, open it in your terminal, and create a file named app.js. Open app.js in your code editor of choice and add the following line:

console.log("Welcome to Node.js!");

Running the Script

To execute this script, you will use the node command followed by the name of the file you want to run. Ensure your terminal is in the same directory as your app.js file, and run:

node app.js

You should see Welcome to Node.js! printed in your terminal.

When you run this command, Node.js reads the script, parses the JavaScript using the V8 engine, executes the logic, and routes the output back to your terminal window.