#
NPM (Node Package Manager)
This tutorial explains to you what the NPM (Node Package Manager) is and how you can use it with Node.js.
A Node.js Module is a library of functions that could be used in Node.js. These modules are generally small and include functionalities related to a domain. For the list of Node.js modules, you can go here.
There are 3 module type in Node.js:
- Built-in modules (provided by Node.js installation)
- User-defined modules (created by the user)
- Third party modules (generally available on the Internet)
You can create your own Node.js modules, but you can use a lot of interesting modules/ packages provided by the Node.js (these are the built-in modules). The management of these module in a Node application are managed by a software named NPM (Node Package Manager).
A package
is one or more modules (libraries) grouped (or packaged) together.
Node Package Manager (NPM) is a command line tool that installs, updates or uninstalls Node.js packages in your application. It is also an online repository for open-source Node.js packages.
If you want to check the NPM version you have installed, run the following commands:
npm -v
or
npm --version
If you want to have the last NPM version you have to run the following command:
npm install npm -g
Info
-g = globally
If you want to add a module/ load a package to your application, you can run a command like this one (in my example I want to load the express framework):
Info
npm init -f
command will create the package.json file in the application folder.npm install express
command will create and populate node_modules with the files needed for express framework.
The package.json file is also updated with the express dependency:
Node Package Manager (NPM) can install/manage packages in 2 ways:
- globally : so that all the node.js application on that computer can import and use the installed packages. Apply
-g
in theinstall
command to install package globally. - locally : so that only a node.js application can import and use the installed packages (modules).
Using the NPM we can also update or remove (uninstall) a package: