Introduction to NPM (NodeJS)

 


      In this article, I introduce you to a  very useful tool - NPM, the official package manager for NodeJS. Before learning about NodeJS let us see why we need a package manager. It is time for a different HelloWorld application.By the way NPM doesn’t stand for Node Package Manager (as is misconception with many). NPM is just NPM.

1. Colorful Hello World
         In the previous tutorial, we displayed the text “Hello World” to the console. But that turned out to be boring -- plain white text. Let us add some colors to it. This will demonstrate the use a popular npm module.
From the previous tutorial, we have our server.js file as:


One way is to use native terminal coloring options by using color specifiers in the text. But that would make our text ugly to see while debugging. 
Another and a much better way is to use an external module. chalk is an awesome library for NodeJS which can colorize any string. 

Now to include the chalk library (hosted on GitHub), you can clone the repository (https://github.com/chalk/chalk) and  require  its index.js file. This would create problems when dealing with tens of hundreds of modules. Also updating a module would require manually replacing the files. 
OR
Use NPM
NPM is Package Manager for NodeJS. It handles all your modules and Project information for you.
To enable npm for our project, we need to initialize our project with npm. For this do this in the project’s root directory:

$ npm init                                                                                                                                          

Fill in the all the information :


  npm init  creates a file  package.json in your project directory. This is a file which describes your project to npm engine. After execution your package.json file should contain the following:
The file contains a JSON object with the following fields:

1. name  - This is the name for your project. By default it is the name of your current folder. 
2. version - The current version of your project.
3. description - Description of project.
4. main - This is the entry point of your app. When others require your module in their project, require will import the file specified by this field.
5. scripts - This is another JSON object containing various scripts. These scripts are used by various deployment services to run different tasks without the need to manually execute each script.
6. keywords - Keywords which enable efficient searching of your module.
7. author - Name and email id of the author of this project.
8.  license - The license under which you are releasing this module.

We are done with initialising our npm project. Next we need to add chalk module to our project.
To install any module from npm repository -- Where all the NodeJS modules are hosted, simple use

$ npm install <module_name>                                                                                                               

If we add --save flag to the above command, then npm adds the module in the list of dependencies in the package.json file.

So, to install chalk module, we do 

The --save flag edits the package.json file to include the new module. All these modules are installed in the  node_module/  directory which have been created by the npm install command. 

Also, your package.json will have added some lines: 
That is how to install modules to our project.
Open server.js file and modify the code as:


In the first line, we tell NodeJS that we want to import all the functions of chalk module with the name chalk. Then we just call chalk.<function name>() whenever we need to use that function.

The chalk module exposes red function to make the string red. Check out the documentation of the modules before using.

On running this script you would get an output like


Great…!! We made the text colorful. That is how we use npm package manager for importing external modules in your application.

No comments:
Write comments

Popular Posts

Find us on Facebook