If you're unfamiliar with REPL Terminal in Node.js, you can explore additional articles available on Mytour. Let's continue reading the content below to gain a better understanding of what NPM in Node.js entails.
Understanding the concept of NPM in Node.js
What is NPM in Node.js?
NPM in Node.js stands for Node Package Manager, performing 2 primary functions, including:
- Providing online repositories of Node.js packages/modules that can be found on search.nodejs.org.
- Offering command-line utilities for installing Node.js packages, managing versions, and dependencies of Node.js packages.
NPM has been integrated since Node.js version 0.6.3. To check the Node.js version you're using, open the console, then enter the command below:
npm --version
If you're using an outdated NPM version in Node.js, you can easily update to the latest version by running the command below with root privileges:
$ sudo npm install npm -g
/usr/bin/npm -> /usr/lib/node_modules/npm/bin/npm-cli.js
[email protected] /usr/lib/node_modules/npm
Using NPM to install modules in Node.js
To install any Node.js module, you use the simple syntax below:
$ npm install
module name
For example, the command below installs a node.js module named express:
$ npm install express
To utilize modules in a js file, use the command below:
var express = require('express');
Installing Global / Local packages in Node.js
By default, NPM installs any dependencies locally on your computer, meaning it installs packages in the node_modules directory within the Node application directory. Locally deployed packages can be accessed through the require() method.
For instance, upon installing the express module, it will generate a node_modules directory in the current folder, where the express module is installed.
$ ls -l
total 0
drwxr-xr-x 3 root root 20 Mar 17 02:23 node_modules
Moreover, you can also utilize the command npm ls to list all installed modules on your local machine.
Open the Node.js command prompt and enter the command npm ls as illustrated below:
Global package/module installations are stored in the system directory. These dependencies can be used in any Command Line Interface (CLI) function in node.js but cannot be imported directly using the require() of the Node application.
Try installing the express module by opening the Node.js command prompt and entering the following command:
npm install express -g
The above syntax will return similar results but the module will be installed globally. The first line will display the module version and the location where the module is installed.
To check all globally installed modules, you use the following command:
npm ls -g
Uninstalling a Module
Use the following command to uninstall a Node.js module:
npm uninstall express
After NPM uninstalls the package, you can verify again by checking the contents of the /node_modules/ directory or alternatively by using the following command:
npm ls
Searching for Modules
To search for a package name, you use NPM:
$ npm search express
Updating a Module
To update package.json and change the version of dependencies, you use the following command:
$ npm update express
Creating a New Module
To initiate a new module, you first need to create a package.json. To create a package.json, you utilize NPM. Essentially, NPM will generate the basic framework of package.json.
The next step involves providing necessary information about the module. You can refer to the steps above in the package.json file to understand the significance of the required information. Once the package.json is created, use the following commands to register with the NPM repository using a valid email address.
To publish the module, you use the following command:
$ npm publish
If everything is working correctly, the module will be published while being stored, and can be accessed using NPM just like any other Node.js modules.
Utilizing package.json
Package.json is displayed in the root directory of any Node application/module and is used to define the properties of a package. The following example opens the package.json of the express package in node_modules/express/:
Properties of Package.json
- Name: package name.
- Version: package version.
- Description: package description.
- Homepage: package homepage.
- Author: the author of the package.
- Contributors: names of contributors to the package.
- Dependencies: list of dependencies, NPM automatically installs the mentioned dependencies in the node_module directory of the package.
- Repository: type of repository and URL of the package.
- Main: entry point of the package.
- Keywords: key terms.
In the above article, Mytour has just introduced you to the detailed information about NPM in Node.js. In the next articles, Mytour will further introduce you to the concept of Callback as well as Event in Node.js.