If you decide to migrate to JavaScript as a backend for your next application, the only option is to use 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 npm
to composer
in PHP.
The purpose of this shot is to teach you how to use npm
effectively.
Prerequisites: Make sure you already have
Node.js
installed on your system.
npm
in a nutshellnpm
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 npm
.
Note: To ensure you have
Node.js
installed on your system, typenode --version
in a shell or terminal window.
To work with npm
, you need to prepare your workspace.
mkdir myProject
Node.js
project: npm init
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 typenpm init -y
.
npm
commandsYou can use the commands below in a terminal:
npm -v
or npm — version
: gets the npm
version npm install -g npm
: updates npm
to the latest releasenpm i <packageName>
or npm install <packageName>
: installs a packagenpm uninstall <packageName>
: uninstalls package packageName
npm outdated
: checks for an updated version of a packagenpm update <packageName>
: updates packageName
npm search <name>
: searches for a packagenpm cache clean — force
: cleans the cachenpm audit
: audits packages for known security vulnerabilitiesnpm ls
: shows installed packagesnpm help
: gets helpNow, we will install dependencies in our project.
First, make sure you have a clean and ready Node.js
workspace.
The package we want to install is called express
, a Node.js
framework.
npm i express
npm
will fetch express
from npmjs.com, which is the default registry it uses. A registry is a public database of JavaScript software.
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 byNode.js
to 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:
package.json
and package-lock.json
filesThe reference for the express
package was written in the package.json
file.
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 package.json
.
Delete node_modules
and run npm install
. The directory will be created again, and all referenced modules in package.json
will be installed.
The 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.
npm
is a JavaScript package manager for Node.js
. To make a Node.js
project, you have to use npm init
.
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.
Happy coding!
RELATED TAGS
CONTRIBUTOR
View all Courses