Skip to main content

Command Palette

Search for a command to run...

Setting Up Your First Node.js Application

Updated
2 min read
Setting Up Your First Node.js Application

Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to execute JavaScript code outside of a web browser, primarily for building server-side and network applications. Built on Google’s V8 JavaScript engine, it enables the use of a single programming language for both frontend and backend development, streamlining workflows and reducing context switching.

Installing Node.js

To setup our first node.js application, we need to download the node.js from the official docs of node.js.

You can visit to this download link https://nodejs.org/en/download

Checking installation using terminal

Once node.js is installed on the local system , try running these commands.

node -v // show the nodejs version, eg. - v22.13.1
npm -v // show the npm version, eg. - 11.5.2

// npm is the package manager for the node.js. 

Understanding Node REPL

Before writing files, let’s understand something powerful:

REPL(Read Eval Print Loop)

Just open the terminal and run node command. With this environment we can run the JS directly in the terminal.

For Example -

Creating first JS file

Create the file with any name and with .js extension.

To create the file directly from the terminal, you can use touch utility.

touch app.js 

This touch utility will only works with unix based system. This utility does not works in the windows.

Add this code in the file :

console.log("Hello World");

Running script using node command

To run the created file we can use this command

node <file_name>.js

replace the <file_name> with your actual file name.

Writing Hello World server

Now just replace the code inside app.js with this -

// app.js
import { createServer } from 'node:http';

const server = createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World!\n');
});

// starts a simple http server locally on port 3000
server.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});

Now run node app.js command.

You just created your first backend server. That all for this article, if you like these articles do follow me on X and github.