If we want to use additional packages not available in the Node core module, we can install them using npm, which is the Node package manager.
npm is automatically installed when we install Node.js. We can use npm to install, update or remove Node packages on our computer and our projects.
The registries are where the packages (modules) are stored. Developers download packages from the npm registry and publish their packages to the registry.
Some essential npm packages:
There are two ways to install a module:
When we install a package globally, it means that we not only install it for our current project, but we can also use it anywhere on our computer.
The packages will be installed in /usr/local.
npm install --global module-name
npm i -g module-name
i is just an alias to install. There’s no difference between i and install.
Example: To install nodemon
npm install -g nodemon
To see the globally installed packages, run this command.
npm ls --depth=0 -global
We install most of our projects’ dependencies locally.
These modules go into the node_modules directory of the local project:
npm install module-name
npm i module-name
To see the installed npm packages with their version, the command is
npm ls --depth=0
In your project root directory, run the update command:
npm update
To test the update, run the outdated command. There should not be any output.
npm outdated
Check which global packages need updating
To see which global packages need to be updated, run the following:
npm outdated -g --depth=0
Update a single global package
To update a single global package on the command line, run the following:
npm update -g <package_name>
Updating all globally-installed packages
To update all global packages, on the command line, run:
npm update -g
The package.json file keeps track of all packages we’ve installed locally to our project and metadata about the project, such as the descriptions, license, location, dependencies, and scripts to build, launch and run.
We can configure many properties, such as:
To use 3rd-party packages in our project, we need to create a package.json file.
To create a package.json file, run the npm init
command.
$ npm init
This will initialize a package.json file.
It will ask us a series of questions about the project, giving a default value in the bracket. We can press “Enter” to accept them or override them by typing a new one.
If you are okay with the default answers to these questions, you can skip the questions and answer yes to all of them automatically by using the -y flag:
npm init -y
npm help json
.npm install <pkg>
For example, to install express:
npm init -y
to initialize the project.npm install express
.const express = require("express");
npm install express@latest
command line to upgrade to the latest module.To list what modules are installed, run npm ls
from your root project location (where you have package.json and node_modules). It will display a tree of dependencies of this current project.
To list all globally installed modules, run: npm ls -g
.
To remove an npm module, use the rm command: npm rm mysql
.
To remove a global module, apply the global flag: npm rm mysql -g.
When we’ve installed a package locally, it will create a node_modules folder that contains all the files and packages that are needed for the package and the dependencies of that package.
However, when people upload their projects on Github, they won’t include the folder because its size is large. So when we download that project, we may need to install that folder again.
We can do it by running this command:
npm install
This command will install all the dependencies listed in the package.json file.