Friday , 23 June 2017
Home / Programming / JavaScript / Node.js / Installing Hapi.js – Node.js Tutorial For Beginners
Installing Hapi.js
Installing Hapi.js

Installing Hapi.js – Node.js Tutorial For Beginners

Let’s get started building our new web application. We’re going to be creating a web blog again, but with more features. For this tutorial, we’ll learn a little more about modules and installing Hapi.js.

NPM

Earlier in this series, we talked about modules. Modules are just JavaScript files with some code that can do perform  tasks like read files, connect to a database or even just do some math calculations. Node.js comes with a couple of modules that are installed globally on your system. You can yaw these modules anywhere as long as Node.js is installed.

I mentioned that you can also create your own modules. Node.js has an official site for developers to upload and download community created modules. The site is called NPMJS. You can find the site by visiting the link below. https://www.npmjs.org/ NPM stands for node packaged modules. Installing modules is very easy and can be done with 1 simple command. We won’t learn this command just yet because we need to learn about a special file called package.json.

package.json file is a file you should always create before starting any Node.js project. It contains information about your project and a list of community related modules you use. This is a very helpful feature as you can specify which version of which modules you need in order for your script to work. If you decide to move servers or share your script with someone, then they don’t have to install the modules all over again. They can just simply run your package.json file and all the modules they need will be installed for them. Let’s create this file now. Make sure you’re in the directory you’ll be creating your blog application. I’m going to be working in the same directory as last time. Run the following command in your command prompt.

The npm command is a command created by Node.js when you installed it. It allows you to perform operations related to modules and packages. One of these operations is the init command. This command will help you in creating a package.json file. The console will give you instructions on how to fill out a package.json file. You should fill out the form like the image below.

Installing HapiJS
Installing HapiJS

package.json file contains many properties, but the npm init command only helps us with the basics. We actually don’t need to use any of the extra properties that the package.json can use. For now, the basics will suffice for our blog application. If you look at your folder, then you’ll see a file created called package.json. Let’s open this file and you should see code similar to the one below.

We could’ve created this file manually, but this handy command can be helpful just in case you forget what properties need to be defined. The properties created are pretty easy to understand just by their names. They don’t help our application at the moment. Let’s learn about installing community created modules.

Installing Community Modules

Installing hapi could never be easier with an npm command. First we have to figure out the package name. You can find the package name by finding the NPM page for the package. The hapi module can be found here at the official NPM site. https://www.npmjs.org/package/hapi

We’re going to run the install command followed by the –save command. Run the command below in your command prompt.

The install command will go to the NPM site and look for the module name you specified after it. You’ll notice right away a lot of HTTP requests. Node is just downloading these files for you. If you were to look at your directory, you’ll find a folder called node_modules. This is where node will install all modules. You should find a folder called hapi inside.

The –save command will rewrite your package.json file for you. It will include hapi as dependency. Open up your package.json file again and you should see something similar to the code below.

A new property called dependencies is created with an object of all the modules this application needs in order to run. Right now, we have hapi as our only dependency, but this will change as you follow this series.

Conclusion

We learned how to install hapi and learn a little more about modules. In a future tutorial, we’ll create our own modules and you’ll learn just how they work. In the next tutorial, we’ll start using Hapi and create a new server. If you would like to learn more about Hapi, then click here.

About Jasko Koyn

Check Also

Asynchronous

Asynchronous – Node.js Tutorial for Beginners

Before we continue onto our blog web site. I would like to talk about asynchronous …

Leave a Reply

Your email address will not be published. Required fields are marked *