Skip to main content

Command Palette

Search for a command to run...

Setting Up Your First Node.js Applications Step by Step.

A Beginner Friendly Guide .

Updated
5 min read
Setting Up Your First Node.js Applications Step by Step.

Installing Node.js .

So let's see how to setup your first node.js project , i will explain it in very simple simple step how to do every think , what first and what second and how to do , means everything i will explain what to do when you want to setup first time backend with node.js.

  • Before discussing how to setup node.js application , prerequisite is you must have to install node in your system , if not please install first , because that is important for running node command , if not install that command throw error, so now i am assuming you have node in you system so let's start now.
  • So first step is that create a new folder folder for application , once folder created open folder in terminal , where run you first command for node.js application , below is command .
npm init -y // for skipping question and answering

npm init // give answer why you create this project 
  • After this command ,you can see new file create with the name pakage.json in you folder, where your all project dependencies and dev dependencies stored.

  • This is basic initialization , your application need more packages means dependencies for example express.js, creating a Rest full API's, so let's see in next section how to do that.

Checking installation using terminal .

  • Meaning of this line means , as above i told use terminal to install package , there you can see installation process , given command installed package or not , it is failed or succeed to install package . let's see how it is word.

  • Again above i told you use express.js for creating API's so let's install express.js package via terminal. below you can see how it is works.

// Command paste in terminal.

npm install express // download latest version.

npm install express@version // download specific version .

See in picture how terminal work when you run any command , how they show status to you , given command package install or not.

Understanding Node REPL .

  • Built in command , read your code line by line and run accordingly, it is very useful because some time we need when i write some line of code my system automatically read that line and execute it , do not write any running command , automatically code run and execute , if it is good move ahead , if error occur then i know where is the error and then resolve it .

  • so you can see it is very important because you don't need to run every time to check is my code work fine perfectly , they automatically run code and show you how's the code you write . So it is very important and useful , automatically listen and execute it .

REPL :-

  1. R :- Read.

  2. E :- Eval.

  3. P :- Print.

  4. L :- Listen.

Creating first JS file .

  • To create first file in node.js , you have to option , before discussing that let's discuss first why we need that first file , in this first file usually we setup a server and listen them at specific port .

First approach we have direct create a file at root in the folder , usually first file name index , otherwise server . because i told you what we do there.

Second approach create a file inside src folder means source folder , where your all own code present . At src folder root create that file , why this approach because some time we need separation that's why

Running Scripts using node command .

  • Scripts means in your project lot's of functionality add after some time , example database , server run command , server build command so how to check everything is work or not , for example do manually in terminal first write command and then file name which one you wanted to run and check it is work or not.

  • But is create a overhead means every time you run command with file name, this is not a standard approach , to solve these kind of problem we write some script command in package.json file , if we want to run some particular command write that command in first package.json and then run that command or check it is work or not .

  • so below i am giving to you a basic script command then you will understand it what i want to say .

      "scripts": {
        "start": "node dist/index.js", 
        "dev": "tsc-watch --onSuccess \"node dist/index.js\"",
        "studio": "drizzle-kit studio",
        "db:generate": "drizzle-kit generate",
        "db:migrate": "drizzle-kit migrate"
      },
    

Writing Hello World Server .

Below is a Hello world server create using express.js , it's very simple i am creating this with following above strategy as discussed.

import express from "express";

async function main(){
    const app = express();

    app.use(express.json());

    const PORT = 3000;

    app.get('/', async (req, res) => {
        return res.status(200).json({message: 'Hello World'})
    })

    app.listen(PORT, () => {
        console.log(`Server Started at PORT : ${PORT}`)
    })

    return app;
}

main();
  • Code is very simple if you see it line by line , i think here no need to explain it , because it is very easy a code.
💡
So with my above explanation i hope you understand what i learn and what's my thought process on Setting Up Your First Node.js Application Step by Step.
Setting up first node.js project