How to use the Node Package Manager

Updated on Mar 13, 2019

NPM stands for Node Package Manager. To understand what the Node Package Manager is used for we need to understand the concept of Packages.

 

For example, if you have written a complex web application in Node.js. The application will consist of a frontend, backend, and a database. If you were using PHP you would have to specify different PHP modules when for example connecting to the database (pdo, mysqli), performing simple calculations(bcmath) and so on. Every PHP module comes with a certain set of functions which the developers are using directly to get certain program logic written. Every function is directly called within the PHP code of the application. However, the function’s body can only be found in the used module.

 

Node.js is not different in that aspect. So to connect to a database you can use the mysql module. To install that module you will have to actually use the npm service so it can make it available in the scope of your application.


To install a npm package(node module), you can use the following command:

 

npm install <module name>

 

where <module name> is the name of the actual module you would like to install. For example:
 

npm install mysql

 

This will install the mysql package, and you can then directly utilize the functions of that package in the javascript code of your application:
 

var mysql = require('mysql');


var con = mysql.createConnection({

  host: "localhost",

  user: "yourusername",

  password: "yourpassword"

});


con.connect(function(err) {

  if (err) throw err;

  console.log("Connected!");

});


 

Note that the npm install command ran without an actual package for installation will look for a package.json file in which all the required packages are listed as dependencies. Creating an empty file which can become your package.json can be done by using:
 

touch package.json

 

If we again take for example the mysql module, its dependency will look like this in a sample package.json file:

{

    "name": "test_package",

    "version": "1.0.0",

    "main": "index.js",

    "description": "An example of nodejs package.json",

    "author": "FastComet",

    "dependencies": {

        "mysql": "2.5.0"

    },

    "private": true,

    "analyze": true,

    "license": "MIT"

}

 

Once you have a valid package.json file with a specified dependency, you will also be able to use the Run NPM Install button located in the project view of the Node.js Selector. This will install all modules from the package.json which are currently not available for your app. If the modules are already installed, you will see an error in the top right corner. Note that you can also edit the package.json file directly from this window by clicking on the edit button.

 

editing the package file for your nodejs app

 

The other, and more preferred way of creating a package.json file is to run the following command and fill the fields subsequentially when prompted:

 

npm init

 

Then you can install a package as well as add it to the package.json file with the same command:

 

npm install <module name> --save

 

As there are hundreds of thousands of packages available via the NPM at any time, you have a rich choice of ways to tackle development hurdles and find the optimal solution for your project and business.

On this page...

    Node.js Hosting

    • Multiple Node Versions
    • 24/7 Node.js Support
    • Free Domain for life
    • Hack-free Protection
    • Fast SSD Storage
    • Free Node.js Transfer
    • Free CloudFlare CDN
    • Immediate Activation
    View More