What is node.js?
Firstly , the javascript was able to run on browsers only and not on the machine.
It could not do things like others programming languages can for example it was not able to create things like web server, that could access the file system and connect to databases.
But all of this had been changed with the introduction of node.
Now with node ,the javascript developers can use it on server side or on machine as opposed to being forced to run it on client in the browser.
how exactly is this possible ?
See Node.js is a javascript runtime built on chrome's V8 javascript engine.And it uses the same engine that is V8.
The runtime is something that provides custom functionality ,various tools and libraries specific to an environment.
The V8 provides various objects and functions to do things like clicking on button ,manipulating DOM etc.
Now neither of that feature make sense for node where we don't have any button or DOM so node does not provide those things .Instead , node(javascript runtime) provides various tools that node developers need libraries for setting up web server ,integrating with file system.
What makes node great?
Node uses an event-driven , non-blocking I/O model that makes it light weight and efficient.
- Even-Driven-The process of registering those callbacks for some event and having them call when some I/O operation or some event is done.
- Non-Blocking I/O model - When there is some I/O operation to perform like fetching the data from database ,it can continue to process with other code while waiting for running those long I/O operation .
- Node.js package ecosystem is the largest ecosystem of open source libraries in the world.
Node Package Manager
_npm is a tool that was actually installed on your machine when a node was installed and give access to everything on npmjs.com _
npmjs.com is the website where you can find all sort of packages that you can need to use in your application for example there is a package to validate the email ,for setting up the server etc.
See there are many things that pretty much every application out there needs to do ,these are core functionalities but not something specific to your application . for example validating your email, sending email etc.
So , we use npm modules to solves those common problems which is indeed the standard in the node community.
To save developers time so that we can spend that time on focusing awesome features that makes our app unique.
Node Modular System
It allows us to load functionalities into our app and use it to do interesting things.
It can be use in three different ways :-
- Core Node Modules
- Third party Modules (available on npmjs.com)
- Modules that you yourself have created.
how can we load node modules ?
The require function helps us to load node modules (whether it is a core node module or third party module or the module that you yourself have created.)
so to load any module we need to call require().
loading core node modules
require('fs')
this return all the stuff from that module and we have to store that in a variable like
const fs = require('fs');
This "fs" is a core node module and so we have learned that how can we load core node module.
loading modules that you youself have created.
In a similar manner you can also load the modules that you yourself have created but we need one more extra thing to load the module created by individual and that extra thing is exporting that functionality.
You can export the function as : module.exports = functionName;
This function resides in file say "fileName";
so we can load this function as :
const functionName = require("./fileName");
and now you can call as functionName()
and if there are multiple functions , you can export as
module.exports = {firstFunction,secondFunction};
now you can load the file in which these function resides in "fileName" as:
const lib = require("./fileName")
and can call functions like
lib.firstFunction()
lib.secondFunction()
loading third party modules (modules that are available on npmjs.com.
We already know now that npm get installed on our machine when we install node and give access to everything on npmjs.com.
To use these modules in our script we have to take two important steps.
Step 1. Initialize npm in our project.
Step 2. Install the module we want to use.
Step 3. load npm module.
First Step:
write npm init in your terminal.
It will create a configuration file that will use to manage all dependencies from the website npmjs.com.
Second Step:
write npm install packageName in your terminal
Now after these two steps we have two folders "node modules" and package-lock.json.
node modules - It contain all the packages that are installed and that package folder contain all the code required to make that package.
package-lock.json - It makes the app faster and more secure ,it contain all the dependencies and the location from where the dependencies are fetched.
Third Step :
const moduleName = require("name of package installed using above two steps").
Top comments (0)