If we want to use additional packages that is not available in Node core module, we can install them using npm, which is the Node package manager.
npm is automatically installed when we installed 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
The 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 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
The package.json file keeps track of all packages we’ve installed locally to our project and meta data about the project as well, such as the descriptions, license, location, dependencies, 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 package.json file.
To create a package.json file, run npm init
command
$ npm init
This will initialize a package.json file.
It will ask us a series of questions about the project and it gives a default value in bracket. To accept them, we can press “Enter” or we can override it 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 -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’s going to create a node_modules folder that contains all the files and packages that are needed for the package and 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 that are listed in the package.json file.