Getting Started with Node.Js in 2022

 Node.Js is open source cross-platform JavaScript runtime environment that runs on V8 engine (C++) that initially built for chrome browser to executes JavaScript code outside browser on the desktop. Along with chrome v8 Libuv (C) supports in interacting underlying computer system to execute JavaScript. JavaScript originally was scripting language for the browser. Now, it has been deployed far beyond. Node.Js played important role in shifting JavaScript from browser to desktop. Node.Js support the execution of JavaScript on the desktop. Node.Js built around event driven, non-blocking I/O model which makes it very efficient in developing fast web servers in JavaScript. 

Getting Started with Node.Js in 2022 - nodejs logo

What is npm?

NPM stands for Node Package Manager. NPM is the manager for the node ecosystem. It manages publicly available node packages from various users. Node package consists JavaScript files together with package.json which is manifest file for the node package. On installing Node.Js in the computer; npm automatically gets installed.


Setting up Node.Js and npm in the device

Here, in this article we provide with step by step procedure to setting up Node.Js and npm in your device. It has been already mentioned that installing Node.Js automatically install npm in the device. So, let's install Node.Js in the device first. Here are the steps;
  1. Search 'Node' in your favorite browser and get into its website or directly into site; nodejs.org
  2. There it would shows two alternatives. Node most people using and Node latest version. Go for Latest/current version for your device and download. The site automatically distinguish your device Mac or Window and shows the option based on that however don't forget to check the OS of your device.
  3. Follow the installation procedure to install node.js providing administrative access.

How to know Node.Js installed in the device?

To check Node.Js installed in the device you have to open terminal window or command window and add the prompt;
node -v (to check version of node installed)
npm -v (to check version of npm installed)

Getting Started with Node.Js in 2022 - check node & npm version installed
Checking node and npm version installed in the device

Now, we are ready to use node.js in the device.

We begin using node.js opening existing project in Editor or creating new project. Let's create a folder in suitable directory, giving suitable name for your project. Get into the project folder and open the project in VS code (recommended). Or, open the terminal and go that directory using cd yourfoldername/projectfoldername.

For opening the project in Visual Studio Code you have to open Visual Studio (close existing project if any) and drag and drop project folder in Visual Studio Code. 

Or,

Get into project folder.
Press Shift and Right-click together, you would get menu with 'Open PowerShell window here' select it.
If you have installed Git in your device you would get 'Git Bash Here' option on right-click getting into the folder. For more about Git (recommended): Getting started with Git and link with GitHub repository

In the Git Bash or in PowerShell window, type command to open the folder in Visual Studio Code. The command is;

    code .



When you open project in the Editor you initially don't have any files. Now, open terminal of the editor and start using node.js. In Visual Studio Code: Terminal --- New Terminal.

Create your first Node Project

When you open your project folder you don't have any files there.
Now, in editor terminal give the command.


    npm init
        OR
    npm init -y


npm init required to manually enter project details while npm init -y gives auto fillup the details. npm init -y (recommended) easier to use. While doing via npm init don't require all the description fill up most of are left default.

After running the given code. You would see package.json file created.
Getting Started with Node.Js in 2022 - Demo Node Project
Demo Node project

By default index.js is the entry point to the node project. Let's now create one from "New file" nearby project name. The symbol to create new file, folder, refresh and collapse appears when hovering mouse nearby project name. 
Name new file with the name 'index.js'. In index.js write the following code.


    const http = require('http');
    const port = 3000;

    const server = http.createServer(function (req, res) {
        res.write('Hello World! This is my first node project');
        res.end();
    })
    server.listen(port, function (error) {
        if (error) {
            console.log('Something went wrong', error);
        } else {
            console.log('Server running on port ' + port);
        }
    })


Save the code and run the project in the terminal with the command


    node index.js


In the browser, type: http://localhost:3000/
When it runs in browser see in the editor terminal, you would see


    PS C:\Users\Amrit\Desktop\Demo> node index.js
    Server running on port 3000

  
In browser, you would get, the text written in res.write(). The is the simple node project.
Every thing you do by editing this index.js file.



How to install node module using npm?

Using npm we install packages required for our project. Let's now install nodemon so, no need to refresh the browser in every change made in index.js. For that, let's give command install nodemon in the terminal.
First, press Ctrl + C to stop the running server.
Then, 

    PS C:\Users\Amrit\Desktop\Demo> npm install nodemon

    added 116 packages, and audited 117 packages in 18s

    15 packages are looking for funding
    run `npm fund` for details

    found 0 vulnerabilities
    PS C:\Users\Amrit\Desktop\Demo>

 
Then, you would see, changes in project directory. There you would see node_modules folder created. Inside there are necessary files for the package and it's dependencies packages. Along with that package-lock.json file created which gives information about the version of each package the nodemon depends on. It would be useful when we need specific version to download for the package to run.

Let's update the package.json and include two scripts, one to start and another for development

 
    "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1",
      "start": "node index.js",
      "dev": "nodemon index.js"
    },


Now, in the terminal we can run npm start, to start the project. We should restart after every changes. So for development, npm run dev will run the node with nodemon, which will detect the change and restart the server.


    npm run dev


Hopefully, the article is helpful in someway. If you still have any query, misunderstanding and found anything wrong above please free to mention below in the comment. Have a nice time. Stay Happy, Stay Healthy, Happy Coding!

0 Comments