Skip to main content

Command Palette

Search for a command to run...

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

Updated
7 min read

1. Installing Node.js

https://images.openai.com/static-rsc-4/4-XW93Bqui_6Q3Ur45LKpTD7iy7GeEi6lBCI477uBRawdgMc2En97fBC0pKNIa1gBJkZtwYSX7pvUL9Cvw6BOtfr4nidkeV09Tdfcr8Xx1MiwUWDcfG8ZwkyIyTKtzgzez38kQbJmK4vFB-nZDQ3fl7uxNubodGf-tCMKq4Z44NAohj0Psr6FMiqAVaxO70a?purpose=fullsize https://images.openai.com/static-rsc-4/qhd1XxVMHTCj3LLEiysxDyLSckH2DyBXq2Kszgnf9yJ0ZNix2Nv6PbhlhNx2ibW_hFXfXwu6JD6DDymlstcVMz-75Pf_JNeavQxmcRlFhbCnpGl8J-laJWoEKISvN8ZwzJmdbq42K9gBYDcZeeIQiTYepWpq6IBCNUxYsu6_7iqs_IY_SFRAXx9buYC4PGnl?purpose=fullsize https://images.openai.com/static-rsc-4/c9b63UxVw6pKZUJcOLazLxX7a2Kx7zpuZs-Qd-_UyWWiJK24dd4IO-mw-3C1ufjuFTv5rVOD_PSJN1Kt4wY1yYId0XSdu9vJlVFbfyIQ9k9FeFasj2ETf27M17MpfddfuA5Nk3FWMqvrUMBabM8TGpWusCh3oROIpMzBgvQmMN5O-T8lIvjwvAJRBSH2kWb0?purpose=fullsize

Before you start building any Node.js application, the first step is to install Node.js on your system. The good news is—it’s very simple and beginner-friendly.

Step 1: Download Node.js

Go to the official website of Node.js.
You’ll see two versions:

  • LTS (Long Term Support) → Recommended for beginners (stable)

  • Current → Latest features but may be less stable

👉 Always download the LTS version.

Step 2: Install Node.js

Once downloaded:

  • Open the installer

  • Click Next → Next → Install

  • Keep everything as default (no need to change anything)

Node.js installation also installs npm (Node Package Manager) automatically, which helps you install libraries later.

Step 3: Verify Installation

After installation, open your terminal (Command Prompt / PowerShell / Terminal) and run:

node -v

If Node.js is installed correctly, you’ll see a version like:

v18.x.x

Now check npm:

npm -v

This will show the npm version.

Example

If both commands return versions, you’re ready to go 🎉

node -v   → v18.17.0
npm -v    → 9.6.7

Why This Matters

Node.js allows you to run JavaScript outside the browser, which means you can build servers, APIs, and full backend applications.

2. Checking installation using terminal

https://images.openai.com/static-rsc-4/c9b63UxVw6pKZUJcOLazLxX7a2Kx7zpuZs-Qd-_UyWWiJK24dd4IO-mw-3C1ufjuFTv5rVOD_PSJN1Kt4wY1yYId0XSdu9vJlVFbfyIQ9k9FeFasj2ETf27M17MpfddfuA5Nk3FWMqvrUMBabM8TGpWusCh3oROIpMzBgvQmMN5O-T8lIvjwvAJRBSH2kWb0?purpose=fullsize https://images.openai.com/static-rsc-4/DyWoqnMT7WdRQyzaf2KbYKvFwjjwtkaXr7GVXKsegVyM17vKvZ67lQXirt8qK-2toe_jtAXK-EhazgH-QYZlt0ysP0e92jWerkfKzGi7N5kSag5M_o7sszAIvtph_mBlrrgihx1ehwJvFF93ae8PGesZYdvVDC07pzk6puv03fVE-NFp9BkuMviMZQuLfsJ2?purpose=fullsize https://images.openai.com/static-rsc-4/T-g_HAKvMVbKuXNzylmzcsZJRis1uXHniv6e54XIvV5OhSqm4z9B4EjMklKjeSt5cXKVLxqhtwlDBHUQ8sB0_f0NSiWA9dqBEUIbMfo_3WB4zfyND237GS6BRddtc6wXOGhicTyyVebFag6q4dfA6ZZkwOOGFcAm_84uP-E-5h6RHA6lKY6V_yjrBvJwM9vv?purpose=fullsize

After installing Node.js, the next step is to make sure everything is working correctly. This is done using the terminal (Command Prompt, PowerShell, or Mac/Linux Terminal).

Step 1: Open Terminal

  • Windows: Open Command Prompt or PowerShell

  • Mac/Linux: Open Terminal

Step 2: Check Node.js Version

Type the following command:

node -v

If Node.js is installed properly, you will see a version number like:

v18.17.0

Step 3: Check npm Version

Now check if npm (Node Package Manager) is also installed:

npm -v

You should see something like:

9.6.7

Example

A successful setup will look like this:

node -v   → v18.x.x
npm -v    → 9.x.x

What If It Doesn’t Work?

  • If you see an error like “node is not recognized”, it usually means Node.js is not installed properly or not added to your system path.

  • In that case, reinstall Node.js or restart your system once.

Why This Step is Important

This quick check confirms that your system can actually run Node.js and install packages using npm. Without this, you won’t be able to build or run your application.

3. Understanding Node REPL

https://images.openai.com/static-rsc-4/CJbKBa7a0NcCrOYxpS9-FKlFSP1MCikss6uMwkAsqOwqbWk3Kw08y0eEA1m7pY9DqjOXjxav4jzRqlKiQcMisYzDwaBQsuIYE5TZmz55n0U2kNHgD8gW6fGatC8Gr9UutvHDCevmpTYE6mxPvLOc2P9m15pLL4S_dvAfbCt6EGsbsTVA8vI6JuL99UvBF41J?purpose=fullsize https://images.openai.com/static-rsc-4/WeHPL8CzF2g5lkuK7f1kgBxNu3Yxd4BFBCIED0UvKqwTDS6xszmMDsy9r04nAgBtsrc4lfoam5i4tZ649w_D9NAxJ5ENoLcACuzNuIKlL9Ou0STnJu8SOqnpxGYCm4LkXxRw7ji8eoEnYNE12hOlB75Xw7SMxuIs4jdpqi0ObOHsUCyBdveiY4dQBSF3GLom?purpose=fullsize https://images.openai.com/static-rsc-4/mlX7qEeNg3m2Llol56izV8xnA0iZmsx2w3ouW-2Fy3YN_3G1_4GqFPQC4xB1GCKw-xj2S0Nc3wZ_0HhjpMQCmaSPke6-EY8YQCkgD1Hi7AKBzZOx-53jbrv2q6Lh4qO3LTPbTwS8-9zMAloJ4Oze2Baj58z_G8rgp0bV8TKa1VwVZWY-XpYjqGdRvoUMTooj?purpose=fullsize

Once you have Node.js installed, you can start using something called the REPL.

REPL stands for:

  • R → Read

  • E → Evaluate

  • P → Print

  • L → Loop

In simple terms, it’s an interactive environment where you can write JavaScript code and see the result instantly.

Step 1: Start the REPL

Open your terminal and type:

node

You’ll see something like:

>

This > symbol means you are now inside the Node REPL.

Step 2: Try Simple JavaScript

You can directly write JavaScript code here:

2 + 3

Output:

5

Or try variables:

let name = "Alok";
name

Output:

'Alok'

Step 3: Use Built-in Features

You can also use JavaScript functions:

Math.sqrt(16)

Output:

4

Useful REPL Commands

  • .help → Shows all available commands

  • .clear → Clears the screen

  • .exit → Exit the REPL

Why REPL is Useful

  • Quick testing of code snippets

  • Learning JavaScript interactively

  • Debugging small logic without creating files

Example Use Case

Suppose you want to test a small function:

function greet(name) {
  return "Hello " + name;
}
greet("Alok")

Output:

'Hello Alok'

No file needed—everything runs instantly.

4. Creating first JS file

https://images.openai.com/static-rsc-4/Gbu3b1TOC_dGwLyNl5DKNch5Q0MVLfHEYln5UVXfFqWCgkxi0JVMn1UnguRIulMCDQUIAWEX32RJFA_n8KLh3aOPNXhgd7chT7Qn6HCHUh4MNGm_FoGxbQrYWyIFBTVILrvzhsL9U9vhx76rkAgmNx0EpDH-lx2AlCpI9RS2bibwmxalMbuwjav26umuW7U6?purpose=fullsize https://images.openai.com/static-rsc-4/mbkJXtU6QEDHbb8c4_VIpP8OTAhDTYt18hMBJ9VGxlwQv4yJpL5RUXoa3ddFkuhfsgbXAYxSrGyByBYpsCBs67VrUMiBR24_wKxIwciuS3ESBcRL61emoBmu9uK7mLayTF4-T8Qut-pl-GOH2bsuhGxSVtyVAbjeJ3WchJSMQMXgnA9Wk0PqZzrmF45gLKUW?purpose=fullsize https://images.openai.com/static-rsc-4/sNdgc88FhJVni0bpoGQr22tEP_u19fq7S_S6WUGSCxXhCQkiIfwMnRiB-q4vfKvqDacHeGnhUKlzI3VcmzpwUUojwM5ynbpmREo849n7Vne8_54kJIwh8EAwiifjTbbcrU425yYikIzNbZSrvXgsGqERcxwVsHwN1y_qFjE60oWGGRd--kofLIgqQ7P2m_gi?purpose=fullsize

Now that you’ve explored the REPL in Node.js, it’s time to create your first actual JavaScript file. This is how real applications are built.

Step 1: Create a File

Open any code editor like VS Code or even Notepad.

Create a new file and name it:

app.js

👉 .js extension is important because it tells Node.js that this is a JavaScript file.

Step 2: Write Your First Code

Add a simple line of code inside app.js:

console.log("Hello, Node.js!");

This will print a message to the terminal.

Step 3: Run the File

Open your terminal in the same folder where your file is saved and run:

node app.js

Output

You’ll see:

Hello, Node.js!

🎉 Congrats! You just ran your first Node.js program.

Example with More Code

Let’s try a slightly bigger example:

const name = "Alok";

function greet(user) {
  return "Welcome " + user;
}

console.log(greet(name));

Output:

Welcome Alok

Why This Step is Important

  • This is how real Node.js apps start

  • You can now write multiple lines of code

  • Helps you build scripts, servers, and full applications

5. Running script using node command

https://images.openai.com/static-rsc-4/sNdgc88FhJVni0bpoGQr22tEP_u19fq7S_S6WUGSCxXhCQkiIfwMnRiB-q4vfKvqDacHeGnhUKlzI3VcmzpwUUojwM5ynbpmREo849n7Vne8_54kJIwh8EAwiifjTbbcrU425yYikIzNbZSrvXgsGqERcxwVsHwN1y_qFjE60oWGGRd--kofLIgqQ7P2m_gi?purpose=fullsize https://images.openai.com/static-rsc-4/Ke9RGvJsHlLfLQyT86vEn12k2I6yxlsYoVJ7uMfRFVsndVP0uCIP5fe7HvnEpZxr_T0kTE7UXT-ZKXIZdw2AD5eCZPZBvmqIuYsE0gaXyxvRA2EEfa6_-Snd1blNq7gFEduXBgXkag9HzjRMGiXHELUbQbtNGVI7HXcX7vylDnpy07jCUVYHrWLGCRXUj24J?purpose=fullsize https://images.openai.com/static-rsc-4/vbTsz-oerxbiRiiimguxNjqZL3aD49JH8g3ghQdfy-_RK7xG-kSRcCUU95G0qjgylaJNoC5kduMcyVvQ0eOhjpJpN2c8PLFavLH74bgMKzbBsT-fOAm4kBuIiLKG2ktz4owzOCtBPyToPGsqrRke16R2S21IBymcRTFvKRYj0-ExAHttbkHUennwGa4Fqy37?purpose=fullsize

After creating your JavaScript file, the next step is to actually run it using Node.js. This is where your code comes to life.

Step 1: Open Terminal in Your Project Folder

Make sure your terminal is opened in the same folder where your file (like app.js) exists.

Example:

cd your-folder-name

Step 2: Run the Script

Use the node command followed by your file name:

node app.js

Output

If your file contains:

console.log("Hello, Node!");

You’ll see:

Hello, Node!

Example with Logic

Let’s try a slightly more useful example:

const a = 10;
const b = 20;

console.log("Sum is:", a + b);

Run it:

node app.js

Output:

Sum is: 30

Common Mistakes

  • ❌ Wrong file name → node app (missing .js)

  • ❌ Wrong folder → terminal not in correct directory

  • ❌ File not saved before running

Pro Tip 💡

You can run any .js file like this:

node filename.js

No need for any extra setup—Node handles everything.

Why This Matters

This command is the core of Node.js development. Whether you're building a small script or a full backend server, you’ll always use node filename.js to run your code.

6. Writing Hello World server

https://images.openai.com/static-rsc-4/n-DX0cRyto01e0uwDaTdxkXj7Atfwmw7G0Lqo3vCHnSS7gf-0YFtP7IG9iYmqDHvdfzPzr4YaUI--KLeRxMpGSATRU0haSOZPI_2VOzc7q8-Ka714hJA7-KG9MKaw57B1hr8Rd3r4i0N6LuUTKBmdAxafbvGBStrleFbMD8o1B8A43JPC0DLFx1kAJ6VHTlJ?purpose=fullsize https://images.openai.com/static-rsc-4/3B3eFj_yoXy-64yYHYd84jJAdR3UoCY0KdCrX7SyrCi-CDJwD2pPl0RYFEoZ-1iQF727tgT0Oie0-Qu_lyGAx_gtufsP292Pe75zdVFhAAhBQ2xoLuk8mP5CTX_i_TfnDrnm7fW5BnRDVw9RsJj2_ub8TFtI1L1iBdaefKccn0Tt7QwgjE9-jAYU64EwXCue?purpose=fullsize https://images.openai.com/static-rsc-4/K3yCN_aXENv27_ESWn3a2IaUTGCEmCMSY9nNy_SubHIMrqnL6-cKzRBYg78Zs_-BtYw9riWw0ClBQUjWp4uy_oq9FNiz_RwOzndPhhcWigtHMb2lkHqZwksrmHbWWKmdwo-YeyWyhz35anRLx0MDMP-cb-_pwwU9NM8I0Fkxpk7dP9zpSBkbVXGTrEH43xwE?purpose=fullsize

Now comes the exciting part—creating your very first server using Node.js. This is how backend development actually begins 🚀

Step 1: Create a New File

Create a file named:

server.js

Step 2: Write Server Code

Add the following code:

const http = require("http");

const server = http.createServer((req, res) => {
  res.end("Hello World");
});

server.listen(3000, () => {
  console.log("Server is running on port 3000");
});

Step 3: Run the Server

Open terminal and run:

node server.js

You’ll see:

Server is running on port 3000

Step 4: Open in Browser

Go to your browser and type:

http://localhost:3000

🎉 You’ll see:

Hello World

How This Works (Simple Explanation)

  • http.createServer() → Creates a server

  • req → Request from client (browser)

  • res → Response sent back

  • res.end() → Sends response and ends request

  • server.listen(3000) → Runs server on port 3000

Example: Custom Message

You can change the message easily:

res.end("Welcome to my first Node.js server!");

Why This is Important

  • This is the foundation of all backend apps

  • APIs, websites, and real-world apps all start like this

  • You now understand how client (browser) and server communicate