Node.js. The software we build always relies on other software, and the node package manager (
npm) lets you easily manage dependencies within your app. You can compare
composer in PHP.
The purpose of this shot is to teach you how to use
Prerequisites: Make sure you already have
Node.jsinstalled on your system.
npmin a nutshell
npm is a dependencies’ manager tool. More precisely,
npm is a
Node.js by default. So, if you have
Node.js installed on your system, you also have
Note: To ensure you have
Node.jsinstalled on your system, type
node --versionin a shell or terminal window.
To work with
npm, you need to prepare your workspace.
The second command will prompt you with some questions that you’ll need to answer.
Once everything is OK, you type
yes and hit the Enter button, and
npm will create a
package.json file with all the information.
Note: If you want to use the default answers from
npm init, you can type
npm init -y.
You can use the commands below in a terminal:
npm — version: gets the
npm install -g npm: updates
npmto the latest release
npm i <packageName>or
npm install <packageName>: installs a package
npm uninstall <packageName>: uninstalls package
npm outdated: checks for an updated version of a package
npm update <packageName>: updates
npm search <name>: searches for a package
npm cache clean — force: cleans the cache
npm audit: audits packages for known security vulnerabilities
npm ls: shows installed packages
npm help: gets help
Now, we will install dependencies in our project.
First, make sure you have a clean and ready
The package we want to install is called
npm i express
npm will fetch
After the package is downloaded,
npm places it in a special directory called
node_modules, and the reference of the module is written in the
package.js file under the dependencies section. You can view the package by entering
ls in the terminal.
node_modules: The default directory used by
Node.jsto find any package it needs. Think of this folder as a local store for your downloaded packages.
Our recently installed
express module is shown below:
The reference for the
express package was written in the
This is the case for any package you install, which is important because it makes it easy for you or someone else to get started with your project in a new instance (be it a computer or another directory).
Additionally, when working with git, you’ll need to use
.gitignore on the
node_modules folder because it is so big and does not belong to you (or your project codebase). The only way to reinstall all packages used in the project will be via
node_modules and run
npm install. The directory will be created again, and all referenced modules in
package.json will be installed.
package-lock.json file helps lock the same version used for your project dependencies. This file guarantees that they have the same version you used when someone is installing your project dependencies.
Node.js. To make a
Node.js project, you have to use
npm install <moduleName> is used to install packages. All installed packages are stored in the
node_modules directory and referenced in
package.json under the dependencies section.
package-lock.json will lock the version of the dependencies you use in the project.
View all Courses