loading...
Cover image for Introduction to Node.js and NPM

Introduction to Node.js and NPM

patilchetan2019 profile image chetan dhanraj patil ・4 min read

Hi folks,
I hope you are doing well and improving your knowledge of coding everyday. Let's dive into our topic.
Working as a software developer I am doing some web based projects in the company. It include developing frontend and backend services. For backend api building I mostly use flask and django frameworks of python.
As I am working on this I always had this curiosity for other backend frameworks like Node.js and express.js .I started to learn about them and thought it would be great to share that knowledge with you in very simple words. We will try to understand Node.js project structure and files inside it and build our simple Node.js app.

Basics :
We are familiar with the javascript as a frontend or browser language which runs on web browsers.We use it to make our web pages more interactive and fulfilling.

As a language javascript is great and fast but how we can run it outside the browser and use for other tasks like server side development.

Here Node.js comes in picture.Node.js allow us to run the javascript outside the browser and use it for other tasks like api building. Inshort it is the javascript runtime.

Short on how node.js do it :
Node.js is a wrapper around javascript engine called as V8 which powers many browser to run javascript including chrome.
So basically it use V8 and add its own other stuff which allow us to run javascript outside the browser in general. Node.js adds thing called as buffer which allow V8 to work with files etc.

How to install Node.js:
Link to install : https://nodejs.org/en/download/
You can go to this link and download Node.js installer(OS based) which include Node.js and NPM (Node Package Manager). Download LTS version which is stable and recommended for most users.

We have covered about Node.js now let's talk about NPM.
In simple terms it is command line tool of Node.js for downloading third party libraries or in node terms dependency.

Why we need this third party libraries in our project and how we can use this npm ?
NPM comes with Node.js bundle so you don't need to install it.
In the real world applications we want to do lots of thing like authentication and encryption which we can do but for this we can use third party popular libraries which are mostly better than code that we will be writing.Because they are used and tested by thousands of developers which gives us evidence of their reliability.

As of now we have little idea about Node.js and its package manager NPM so there no better time than this to create your first Node.js project and practically do the things.

Let's create first project
Steps :

  • create directory called as first_node_project.
  • Move to first_node_project using cd (change directory command)
  • Inside first_node_project create file named as index.js which will be our primary working file.
  • Run the command npm init -y or npm init on terminal.
  • First command will create package.json file with default settings and second will ask developer for information to put inside the package.json .
  • This commands will create file called as package.json.
  • Open your index.js file and put following code
  console.log("hello world"); 
  • Now open your package.json file and add "start": "node index.js" inside the "scripts" object like following
{
  "name": "node_js_beginner",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    **"start": "node index.js"**
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
  • Run the command npm start you will output as following Alt Text

we can use node index.js command directly to run our index.js file but we are doing it using npm and for that we set the node index.js on the start script inside scripts object of package.json

Great you have built your first Node.js project congrats.

Let's talk package.json :
package.json is created when we run the command npm init.
It is project specific file. Simply it contains the setting of your project. It includes following things like name , description of your project and npm command settings inside scripts. etc.
In future when you will install third party libraries in your project this package.json will contain the name of your dependencies and development dependencies.

Break down of package.json :
script object : Using scripts we can automate the repetitive tasks. We set them and assign meaning or kind of task them then we run them using npm command.
Ex.Above created script called as start and assign the node index.js on it. Then we just use the npm start to run our code.
This is how you can set scripts like test , build etc.
Custom scripts : scripts like start , test are special. NPM recognizes them and attached special meaning to them. But we can also write our custom scripts like this "listen": "nodemon index.js". We can't run custom script directly like npm listen for them to run we need use run flag. So we can run above script like npm run listen.

Dependency : Above I mentioned two kind of dependencies development dependencies and dependencies. Development dependencies are dependencies that we require only for development of the application we don't use them on deployment.
Normal dependencies or dependencies which are essentials for our app to run so we require them after deployment also.

To install dependency : npm install dependency-name
To install dev dependency : npm install dependency-name --save-dev

Thank you so much.
In the next blog I will show you how you can build APIs with Node.js and expressjs.

Try to create and play with the project rather than engaging with lots of theory. It will help you to better understand the technology

Posted on by:

patilchetan2019 profile

chetan dhanraj patil

@patilchetan2019

Full stack developer working on chat bot and angular

Discussion

markdown guide