As a Node.js developer, you’ll need several tools in addition to Node.js. This is especially true when you need to develop front-end components.
These tools are known as packages.
You use Node Package Manager (NPM) to install and manage this packages in a useful manner. In addition to providing for installation of packages, NPM also provides a useful interface to work with these packages.
The two thing NPM provides are:
- Online package repositories
- Command-line utility
We would now examine the basics of NPM. I would take you through how to install packages in local and global mode. Also, you will learn how to install, delete and update a package based on version. Then there’s something called package.json which is used to manage dependencies. You’ll learn that too.
Feel free to watch the video lesson as well if you want.
First, you may have to check if NPM is already installed. To do that, use the command:
$ npm --version 2.8.1
If you see the version number as shown above, then node.js is installed. But if not, then you need to install it using the command below:
$ sudo npm install npm -g
After installation, you may also have to update it to the latest version. To do that, use the command below.
sudo npm install npm@latest -g
Installing Packages with NPM
To install a module/package, type the command below, with the name of the module
$ npm install <Module_Name>
$ npm install uglify-js
Then to use this module in your program, you also have to write this code:
var uglify= require('uglify-js');
Global and Local Mode
Packages are installed in Local Mode by default. This means that the package is installed in the directory that exists where Node.js is present. The directory is called node_modules. So to use a local package, we must use the require() method.
Also, you can use the ls command to view all the locally installed modules.
On the contrary, Global packages are placed in the system directory. They can be used in the command-line interface(CLI) but not importable using the require() function in Node. To install a package in global mode, use the switch –global or -g.
For example, the previous package can be installed in global mode like this:
$ npm install uglify-js --global
Now, to list the modules available globally, use the command:
$ npm ls -g
package.json file is used to define the properties of a package. It is available in the root directory of any Node application.
Exercise: Open a package.json file and view the content
Properties of Package.json
- name − the name of the package
- version − the version of the package
- description − the description of the package
- homepage − the homepage of the package
- author − the author of the package
- contributors − the names of the contributors to the package
- dependencies − a list of dependencies.The dependencies mentioned here are installed by default in the node_module folder of the package.
- repository − a repository type and URL of the package
- main − entry point to the package
- keywords − set of keywords
To Uninstall a Module, use the command below:
$ npm uninstall uglify-js
Once a module is uninstalled, then you can check by examining the content of the node_modules directory using the ls command
To Update a Module:
When a module is updates, the version of the dependencies is upateded to the latest version. The command below updates a module:
$ npm update uglify-js
To Search for a Module, use the command:
$ npm search uglify-js
Creating a Module
This means that you need to generate a package.json file. This you can also do using NPM. The command below does it
$ npm init
Then follow the instructions.
At the end, you will then execute:
$ npm publish
to finally publish the module