Day 15 of 100days of code.

Day 15 of 100days of code.

ยท

4 min read

I am delving into the intricacies of building RESTful APIs, refining my skills in middleware usage, and exploring the nuances of asynchronous JavaScript. Lets Goooo๐Ÿ’ช๐Ÿผ๐Ÿš€

Fundamentals of nodeJS

What is NPM

NPM stands for node package manager. It is both a command line interface and a package repository from where we can install and manage packages. The NPM command line is automatically installed in our machine when we install nodeJS. npmjs.com is the package repository where all the packages are stored and from there we can download and install them. The NPM repository is the largest software registry in the world, it has around 1 million packages, libraries and frameworks. NPM is used across the entire JavaScript development whether frontend or backend.

NPM command line interface is a software that we used to manage and install third party open source packages that we choose to use and include in our project.

We can see the version of NPM we have installed in our machine by running the command npm -v in the command line. We can also install packages in the NPM repository using the command line interface.

Types of packages and installs

When we install a package, it can be classified into 2 categories. The regular dependency and the development dependency. If our code is dependent on the package then its a regular dependency. And the dependency which the working of our code is not dependent on the package is the development dependency. The development dependency helps in increasing our productivity.

To install packages, we use the command: npm install package-name . To specify a particular version of the package we add an @ symbol after the package name and a number signifying the version we want to install. i.e : npm install package-name@4But without the @symbol npm NPM automatically installs the latest version of the package.

Lets install the express package, run: npm install express Installing a package automatically adds a node_modules folder and a package.json file to the application folder and file tree.

Inside the package.json file, we notice a new field called dependencies have been created and we have our package listed inside. The express package in this case is a regular dependency ๐Ÿ‘‡๐Ÿผ

What ever package we download and install from NPM repository we have it stored inside the node_modules folder. Once we install a package, that package and all its dependencies will be stored in the node_modules folder. And also keep in mind that when deploying our web application we don't deploy the node_modules folder.

We need some dev dependencies in other to increase productivity. Lets go ahead and install a dev dependency. We need the nodemon package. The nodemon package helps us to automatically save our changes. We need the nodemon package only for development purpose. In order to specify nodemon package as a dev dependency, Lets run the command: npm install nodemon --save-dev. This --save-dev flag indicates that we want to install the package as a dev dependency.

From our package.json file, we see that the nodemon package have be installed in our application as a dev dependency. ๐Ÿ‘‡๐Ÿผ

We have 2 types of package installs

  • Local install

  • Global install

The Local install simply means the package is only available in that project folder and cannot be accessed outside of the folder

The Global install simply means the package is installed globally in the machine and can be accessed from any folder or any project directory. The express and nodemon packages was installed locally that means they cannot be accessed outside of the project folder.

There are some packages that we might need to install globally. The nodemon package is one of those packages. We might need the package for all of our node projects. So nodemon is a tool that helps us develop node JS applications by automatically restarting the node application whenever we change something in our file or working directory, this way we get to save lots of time because we don't have to manually save our file whenever we make any changes.

Lets install the nodemon package globally and as a dev dependency as well. We run the command :npm install -g nodemon--save-dev .

To use the nodemon package run the command: nodemon file-name. And whenever we make any changes in our file, nodemon updates and save the changes automatically๐Ÿ‘‡๐Ÿผ

Now we don't have to restart our sever manually, it automatically updates and saves the changes.

That's all for this lesson.

Next: Architectures of nodeJS runtime

See you soon๐Ÿ˜Š๐ŸซกโœŒ๐Ÿผ...

ย